2. 分别从京东、淘宝和拼多多上查找同一件商品的价格(商品型号自定),分别采集10行数据存入文本文件(格式自定),用爬虫的方式爬取数据
时间: 2024-03-29 08:38:02 浏览: 107
以下是一个简单的Python爬虫程序,可以从京东、淘宝和拼多多上爬取同一件商品的价格,并将数据存入文本文件中:
```python
import requests
from bs4 import BeautifulSoup
# 商品名称和链接
product_name = "iPhone 12"
jd_url = "https://search.jd.com/Search?keyword=" + product_name
tb_url = "https://s.taobao.com/search?q=" + product_name
pdd_url = "https://yangkeduo.com/search_result.html?search_key=" + product_name
# 请求头,模拟浏览器请求
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
# 京东
jd_res = requests.get(jd_url, headers=headers)
jd_soup = BeautifulSoup(jd_res.text, "html.parser")
jd_items = jd_soup.select("#J_goodsList .gl-item")
# 淘宝
tb_res = requests.get(tb_url, headers=headers)
tb_soup = BeautifulSoup(tb_res.text, "html.parser")
tb_items = tb_soup.select(".item.J_MouserOnverReq")
# 拼多多
pdd_res = requests.get(pdd_url, headers=headers)
pdd_soup = BeautifulSoup(pdd_res.text, "html.parser")
pdd_items = pdd_soup.select(".goods-list .goods-item")
# 存储数据到文本文件
with open("prices.txt", "w", encoding="utf-8") as f:
f.write("京东价格:\n")
for item in jd_items[:10]:
name = item.select(".p-name em")[0].text.strip()
price = item.select(".p-price i")[0].text.strip()
f.write(name + ": " + price + "\n")
f.write("\n淘宝价格:\n")
for item in tb_items[:10]:
name = item.select(".title")[0].text.strip()
price = item.select(".price")[0].text.strip()
f.write(name + ": " + price + "\n")
f.write("\n拼多多价格:\n")
for item in pdd_items[:10]:
name = item.select(".goods-name")[0].text.strip()
price = item.select(".price .num")[0].text.strip()
f.write(name + ": " + price + "\n")
```
运行程序后,会在当前目录下生成一个名为“prices.txt”的文本文件,其中包含了从京东、淘宝和拼多多上爬取的商品价格数据。你可以根据自己的需要修改代码中的商品名称和链接,以及数据存储的格式。
阅读全文