python爬虫淘宝水果荔枝商品信息
时间: 2023-11-21 22:04:27 浏览: 56
为了爬取淘宝上荔枝商品的信息,你可以使用Python中的requests和BeautifulSoup库。首先,你需要使用requests库向淘宝网站发送请求,然后使用BeautifulSoup库解析HTML页面,从而获取所需的信息。以下是一个简单的爬虫代码示例,可以帮助你开始爬取淘宝上的荔枝商品信息:
```python
import requests
from bs4 import BeautifulSoup
def get_litchi_info():
# 设置请求头信息
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://s.taobao.com/search?q=%E8%8D%94%E6%9E%9D&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20210722&ie=utf8'
# 发送请求
response = requests.get(url, headers=headers)
# 解析HTML页面
soup = BeautifulSoup(response.text, 'html.parser')
# 获取商品列表
items = soup.find_all('div', class_='item J_MouserOnverReq ')
# 遍历商品列表,获取商品信息
for item in items:
# 获取商品标题
title = item.find('div', class_='title').text.strip()
# 获取商品价格
price = item.find('div', class_='price g_price g_price-highlight').text.strip()
# 获取商品销量
sales = item.find('div', class_='deal-cnt').text.strip()
# 输出商品信息
print('商品名称:', title)
print('商品价格:', price)
print('商品销量:', sales)
print('------------------------')
get_litchi_info()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.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)