下面是一个示例程序,用于爬取淘宝商品详情数据:

import requests
from bs4 import BeautifulSoup

def get_product_details(url):
    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.text, 'html.parser')

    # 获取商品标题
    title = soup.select('.tb-main-title')[0].text.strip()

    # 获取商品价格
    price = soup.select('.tm-price')[0].text.strip()

    # 获取商品销量
    sales = soup.select('.tm-ind-sellCount .tm-count')[0].text.strip()

    # 获取商品评价数
    reviews = soup.select('#J_ItemRates .tm-count')[0].text.strip()

    # 获取商品详情图片链接
    images = [img['src'] for img in soup.select('#J_DivItemDesc img')]

    # 输出结果
    print('商品标题:', title)
    print('商品价格:', price)
    print('商品销量:', sales)
    print('商品评价数:', reviews)
    print('商品详情图片链接:', images)

if __name__ == '__main__':
    url = 'https://item.taobao.com/item.htm?spm=a230r.1.14.1.5e112a93k2hQIQ&id=xxxxxx'
    get_product_details(url)

在代码中使用了requestsBeautifulSoup库,其中requests用于发送HTTP请求,而BeautifulSoup用于解析HTML页面。

在运行代码之前,你需要先安装requestsBeautifulSoup库,你可以使用以下命令安装:

pip install requests
pip install beautifulsoup4

在代码中,get_product_details函数接收一个淘宝商品详情页面的URL作为参数,并从该页面中提取商品的标题、价格、销量、评价数和详情图片链接。

你只需要将url变量替换为你需要爬取的商品详情页面的URL,然后运行代码即可。

请注意,网页的HTML结构可能会随时间而变化,所以你可能需要根据最新的页面结构调整代码。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐