爬取京东商城商品信息代码
时间: 2024-05-02 07:15:14 浏览: 15
当然,以下是一个简单的Python代码示例,用于爬取京东商城商品信息:
```python
import requests
from bs4 import BeautifulSoup
def get_jd_product_info(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取商品标题
title = soup.find('div', class_='sku-name').text.strip()
# 获取商品价格
price = soup.find('span', class_='p-price').find('span', class_='price').text.strip()
# 获取商品评价数量
comment_count = soup.find('a', id='comment-count').text.strip()
# 获取商品详情
detail = soup.find('div', class_='p-parameter').text.strip()
# 返回商品信息
product_info = {
'title': title,
'price': price,
'comment_count': comment_count,
'detail': detail
}
return product_info
# 示例使用:爬取iPhone 12的信息
url = 'https://item.jd.com/100012043978.html'
product_info = get_jd_product_info(url)
print(product_info)
```
请注意,这只是一个简单的示例代码,实际爬取网页时可能需要处理更多的异常情况和反爬机制。另外,爬取网页信息需要遵守网站的相关规定和法律法规,确保合法合规使用。