Python爬虫后解析数据,同时让两组数据一起导入Excel文件的代码
时间: 2024-06-01 13:03:11 浏览: 102
您好!感谢您使用 ChitGPT 进行对话。以下是一段 Python 爬虫后解析数据、同时让两组数据一起导入 Excel 文件的代码示例:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 爬取第一组数据
url1 = "https://www.example.com/page1"
r1 = requests.get(url1)
soup1 = BeautifulSoup(r1.text, "html.parser")
data1 = []
for item in soup1.find_all("div", class_="item"):
title = item.find("a").get_text()
link = item.find("a")["href"]
date = item.find("span", class_="date").get_text()
data1.append({"title": title, "link": link, "date": date})
# 爬取第二组数据
url2 = "https://www.example.com/page2"
r2 = requests.get(url2)
soup2 = BeautifulSoup(r2.text, "html.parser")
data2 = []
for item in soup2.find_all("div", class_="item"):
title = item.find("a").get_text()
link = item.find("a")["href"]
date = item.find("span", class_="date").get_text()
data2.append({"title": title, "link": link, "date": date})
# 合并两组数据
merged_data = []
for i in range(len(data1)):
merged_data.append({"title_1": data1[i]["title"], "link_1": data1[i]["link"], "date_1": data1[i]["date"], "title_2": data2[i]["title"], "link_2": data2[i]["link"], "date_2": data2[i]["date"]})
# 导出到 Excel 文件
df = pd.DataFrame(merged_data)
df.to_excel("data.xlsx")
```
希望能够帮助到您!如果您还有其他问题或需求,请继续提出。
阅读全文