淘宝商品信息python爬虫代码
时间: 2024-01-08 13:01:59 浏览: 97
以下是一个简单的淘宝商品信息爬虫的Python代码示例:
```python
import requests
from bs4 import BeautifulSoup
def get_taobao_products(keyword):
url = f'https://s.taobao.com/search?q={keyword}'
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'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
products = []
for item in soup.select('.JIIxO'):
title = item.select('.title')[0].text.strip()
price = item.select('.price')[0].text.strip()
products.append({'title': title, 'price': price})
return products
keyword = '手机'
products = get_taobao_products(keyword)
for product in products:
print(product['title'], product['price'])
```
这段代码使用了requests库和BeautifulSoup库来发送HTTP请求和解析HTML页面。它通过模拟浏览器发送GET请求,然后使用BeautifulSoup解析返回的HTML页面,提取出商品的标题和价格信息,并将其存储在一个列表中。最后,它遍历列表并打印出每个商品的标题和价格。
请注意,这只是一个简单的示例代码,实际的淘宝商品信息爬虫可能需要更复杂的逻辑和处理。此外,爬取淘宝网站的数据可能涉及到反爬虫机制,需要注意遵守网站的使用规则和法律法规。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)