爬取https://top.zol.com.cn/compositor/16/notebook.html和https://top.zol.com.cn/compositor/16/manu_attention.html 需要爬取名称,品牌,价格,评分
时间: 2023-12-20 16:03:36 浏览: 36
好的,以下是Python代码,需要安装requests、beautifulsoup4和pandas库:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 爬取第一个网页
url1 = "https://top.zol.com.cn/compositor/16/notebook.html"
resp1 = requests.get(url1)
soup1 = BeautifulSoup(resp1.text, "html.parser")
items1 = soup1.find_all("div", class_="list-item")
# 爬取第二个网页
url2 = "https://top.zol.com.cn/compositor/16/manu_attention.html"
resp2 = requests.get(url2)
soup2 = BeautifulSoup(resp2.text, "html.parser")
items2 = soup2.find_all("div", class_="list-item")
# 保存数据到Excel文件
data = []
for item in items1 + items2:
name = item.find("h3").text.strip()
brand = item.find("span", class_="item-brand").text.strip()
price = item.find("b").text.strip()
score = item.find("em").text.strip()
data.append([name, brand, price, score])
df = pd.DataFrame(data, columns=["名称", "品牌", "价格", "评分"])
df.to_excel("data.xlsx", index=False)
print("数据已保存到data.xlsx文件")
```
以上代码会将爬取的数据保存到当前目录下的data.xlsx文件中,你可以根据自己的需求修改保存的文件名和路径。