下面是一个使用BeautifulSoup和Selenium的示例代码,用于从网页中提取数据并将错误数据复制到电子表格中:
from bs4 import BeautifulSoup
from selenium import webdriver
import pandas as pd
# 初始化webdriver
driver = webdriver.Chrome()
# 打开网页
driver.get("https://example.com")
# 获取网页源代码
html = driver.page_source
# 使用BeautifulSoup解析网页源代码
soup = BeautifulSoup(html, "html.parser")
# 创建一个空的电子表格
df = pd.DataFrame(columns=["Title", "Price"])
# 提取数据并将正确的数据添加到电子表格中
for item in soup.find_all("div", class_="item"):
try:
# 提取标题和价格
title = item.find("h2").text
price = item.find("span", class_="price").text
# 将数据添加到电子表格中
df = df.append({"Title": title, "Price": price}, ignore_index=True)
except:
# 如果出现异常,将错误数据复制到电子表格中
df = df.append({"Title": "Error", "Price": "Error"}, ignore_index=True)
# 关闭webdriver
driver.quit()
# 将电子表格保存为CSV文件
df.to_csv("data.csv", index=False)
这个示例代码通过使用Selenium打开一个网页,并使用BeautifulSoup解析网页源代码来提取数据。如果在提取数据时出现异常,将错误数据复制到电子表格中。最后,将电子表格保存为CSV文件。请注意,在运行此代码之前,需要先安装BeautifulSoup、Selenium和pandas库,并根据需要将webdriver更改为您所使用的浏览器驱动程序。