python爬虫爬取京东商品信息代码
时间: 2024-06-16 12:02:27 浏览: 28
Python爬虫用于从网站上抓取数据,京东的商品信息可以通过Scrapy框架或者requests库配合BeautifulSoup等库来实现。以下是一个基本的Python爬虫代码示例,使用了requests和BeautifulSoup来获取商品标题和价格:
```python
import requests
from bs4 import BeautifulSoup
# 定义京东商品URL
url = 'https://search.jd.com/Search?keyword=example_product&enc=utf-8&qrst=1'
def get_jd_item_info(url):
# 发送GET请求
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
# 解析HTML内容
soup = BeautifulSoup(response.text, 'lxml')
# 找到商品列表
item_list = soup.find_all('li', class_='gl-item')
for item in item_list:
# 提取商品标题和价格信息(这里只是一个示例,实际结构可能会有所不同)
title = item.find('div', class_='p-name').find('a').text
price = item.find('i', class_='price').text
print(f"商品标题: {title}, 价格: {price}")
else:
print(f"请求失败,状态码: {response.status_code}")
get_jd_item_info(url)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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://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)