Python爬虫:获取京东商城商品信息

本文分享了使用Python和Selenium爬取京东商城商品详细信息的实战经验,包括商品价格、名称、评论数、性价比、店铺及商品详情页指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

爬取京东商城商品信息

爬取 :商品价格  商品名 商品评论数  商品购买值  店铺  详情页商品指标

直接上代码:
from selenium import webdriver
import requests
from multiprocessing.pool import Pool
from lxml import etree
#爬取京东商城
browser = webdriver.PhantomJS()

def save(content):
    with open('television.doc', 'a', encoding='gbk')as f:
        f.writelines(content)

def paser_index(url):
    browser.get(url)
    selector = etree.HTML(browser.page_source)
    items = selector.xpath('//li[@class="gl-item"]')
    for item in items:
        price = '商品价格/元:'+item.xpath('.//strong[@class="J_price"]//i/text()')[0]
        title = str(item.xpath('./div/div[3]/a/em/text()')[0]).strip()
        comment = '评论数:'+item.xpath('.//div[@class="p-commit p-commit-n"]//a/text()')[0]
        price_buy = item.xpath('.//span[@class="buy-score"]/em/text()')
        if price_buy:
            price_buy = '商品性价比:'+price_buy[0]
        else:
            price_buy = 'none'
        shop = item.xpath('.//div[@class="p-shop"]/span/a/text()')[0]
        href = 'https:' + item.xpath('.//div[@class="p-img"]/a/@href')[0]
        print(price + '\n', title + '\n', comment + '\n', price_buy + '\n', shop + '\n')
        info = [price, title, comment, price_buy, shop]
        save('\n'.join(info))
        content = get_detail(href)
        detail_paser(content)


def get_detail(url):
    #获取商品详情页
    response = requests.get(url)
    return response.text

def detail_paser(content):
    #解析商品详情页
    details = etree.HTML(content)
    lis = details.xpath('//ul[@class="parameter2 p-parameter-list"]//li')
    for li in lis:
        prodrcts = li.xpath('.//text()')[0]
        save(prodrcts+'\n')
        print(prodrcts)
if __name__ == '__main__':
    #多线程爬取
    print('开始。。。。。。。。。。。。。。。。。。。。。。。。')
    urls = ['https://list.jd.com/list.html?cat=737,794,798&page={}'.format(str(i)) for i in range(1, 3)]
    pool = Pool()
    result = pool.map(paser_index, urls)
    pool.close()
    pool.join()

爬取结果如下:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影雀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值