爬取京东网站数据 python代码
时间: 2023-08-27 13:04:04 浏览: 96
以下是爬取京东商品数据的 Python 代码示例:
import requests
from bs4 import BeautifulSoup
# 设置请求头信息,模拟浏览器访问
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.36 Edge/16.16299'
}
# 定义爬取函数
def get_jd_items(keyword):
# 构造请求 URL
url = 'https://search.jd.com/Search'
params = {
'keyword': keyword,
'enc': 'utf-8',
'wq': keyword,
'page': '1'
}
# 发送 GET 请求获取页面内容
response = requests.get(url, params=params, headers=headers)
# 使用 BeautifulSoup 解析页面内容
soup = BeautifulSoup(response.text, 'html.parser')
# 获取商品列表
items = soup.select('#J_goodsList > ul > li')
# 遍历商品列表,提取商品信息
for item in items:
# 商品名称
name = item.select('.p-name > a > em')[0].text
# 商品价格
price = item.select('.p-price > strong > i')[0].text
# 商品链接
link = item.select('.p-name > a')[0]['href']
# 商品图片链接
image = item.select('.p-img > a > img')[0]['src']
# 打印商品信息
print('名称:', name)
print('价格:', price)
print('链接:', link)
print('图片:', image)
print('-' * 50)
# 调用爬取函数
get_jd_items('手机')
上述代码会爬取京东搜索页面中第一页的手机商品信息,可以根据需要修改关键词和页码等参数。需要注意的是,网站的反爬机制可能会对请求进行限制,可以通过设置请求头信息等方式进行规避。
相关推荐


















