python爬虫爬取京东商品,给出代码
时间: 2024-12-16 14:11:50 浏览: 85
在Python中爬取京东商品信息通常会使用`requests`库来发送HTTP请求获取网页内容,然后使用如`BeautifulSoup`或`lxml`这样的HTML解析库来解析网页结构。以下是一个简单的例子,展示如何抓取京东商品的基本信息:
```python
import requests
from bs4 import BeautifulSoup
# 定义京东商品详情页的URL
url = "https://item.jd.com/{jd_id}.html" # {jd_id}替换为你想爬取的具体商品ID
def get_jd_item(jd_id):
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.format(jd_id=jd_id), headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'lxml')
# 找到商品基本信息,这里只是一个示例,实际结构可能会变化
title = soup.find('div', {'class': 'J_title'}).find('h1').text.strip()
price = soup.find('span', {'class': 'p-price'}).text.strip()
return {"title": title, "price": price}
else:
print(f"Failed to fetch page with status code {response.status_code}")
return None
# 调用函数并打印结果
jd_item = get_jd_item("你的商品ID")
if jd_item:
print("商品标题:", jd_item["title"])
print("商品价格:", jd_item["price"])
else:
print("未找到商品信息")
阅读全文
相关推荐


















