下面是使用Python爬取京东商品数据的示例代码:

import requests
from bs4 import BeautifulSoup

url = "https://search.jd.com/Search?keyword=python"
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, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
items = soup.find_all("li", class_="gl-item")

for item in items:
    name = item.find("div", class_="p-name").a.em.text.strip()
    price = item.find("div", class_="p-price").strong.i.text.strip()
    print(name, price)

这个代码从京东上搜索“python”关键字,并打印出每个商品的名称和价格。我们使用requests库来发送HTTP请求并获取响应,然后使用BeautifulSoup库来解析HTML内容。我们找到每个商品的名称和价格,然后打印出来。

要注意的是,我们需要设置一个合适的User-Agent头,以避免被京东网站防爬虫机制拦截。我们也可以使用代理服务器来进一步隐藏我们的IP地址。

使用python爬取京东的商品数据。

原文地址: https://www.cveoy.top/t/topic/rxp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录