Python 淘宝猜你喜欢数据爬取
时间: 2023-11-05 18:38:07 浏览: 61
好的,以下是一个简单的Python爬虫示例代码,用于爬取淘宝猜你喜欢的商品信息:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头部信息,模拟浏览器请求
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'}
# 构造个人推荐页面的 URL
url = "https://i.taobao.com/my_taobao/api/ajax_user_guess_you_like.do"
# 发送请求并获取 HTML 源码
response = requests.get(url, headers=headers)
html = response.text
# 解析 HTML 源码,提取推荐商品的 ID
soup = BeautifulSoup(html, 'html.parser')
ids = soup.select('.item-pic a')
id_list = []
for id in ids:
item_id = id['data-nid']
if item_id not in id_list:
id_list.append(item_id)
# 构造推荐商品的详细信息页面的 URL
detail_url = "https://item.taobao.com/item.htm?id="
# 发送请求并获取推荐商品的详细信息
for id in id_list:
response = requests.get(detail_url + id, headers=headers)
html = response.text
# 解析 HTML 源码,提取推荐商品的详细信息
soup = BeautifulSoup(html, 'html.parser')
title = soup.select('.tb-main-title')[0].text.strip()
price = soup.select('.tb-rmb-num')[0].text.strip()
print("商品名:", title)
print("价格:", price)
```
这段代码可以爬取淘宝猜你喜欢的商品信息,包括商品名和价格,并输出到控制台中。注意,在使用该代码时需要设置请求头部信息,避免被反爬虫机制封禁。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)