空空12315 2019-03-15 10:56 采纳率: 100%
浏览 747
已采纳

爬虫失败 - 京东搜索结果

导出的结果是一张只有表头没有数据的空表。

#信息采集:名称、价格、评论数、商家名称等
import requests
from lxml import etree
from pandas import DataFrame
import pandas as pd

jdInfoAll=DataFrame()
for i in range(1,4):
    url="https://search.jd.com/Search?keyword=bosch&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&suggest=1.his.0.0&ev=exbrand_%E5%8D%9A%E4%B8%96%EF%BC%88BOSCH%EF%BC%89%5E&page="+str(i)
    res=requests.get(url)
    res.encoding='utf-8'
    root=etree.HTML(res.text)
    name=root.xpath('//*[@id="J_goodsList"]/ul/li[@class="gl-item"]/div/div[@class="p-name p-name-type-2"]/a/em/text()[2]')
    for i in range(0,len(name)):
        name[i]=re.sub('\s','',name[i])
        print(i)

    #sku
    sku=root.xpath('//*[@id="J_goodsList"]/ul/li/@data-sku')
    print(sku)

    #价格
    price=[]
    comment=[]
    for i in range(0,len(sku)):
        thissku=sku[i]
        priceurl="https://p.3.cn/prices/mgets?callback=jQuery6775278&skuids=J_"+str(thissku)
        pricedata=requests.get(priceurl)
        pricepat='"p":"(.*?)"}'
        thisprice=re.compile(pricepat).findall(pricedata.text)   
        price=price+thisprice

        commenturl="https://club.jd.com/comment/productCommentSummaries.action?my=pinglun&referenceIds="+str(thissku)
        commentdata=requests.get(commenturl)
        commentpat='"CommentCount":(.*?),"'
        thiscomment=re.compile(commentpat).findall(commentdata.text)
        comment=comment+thiscomment

    #商家名称
    shopname=root.xpath('//*[@id="J_goodsList"]/ul/li[@class="gl-item"]/div/div[@class="p-shop"]/span/a/@title')
    print(shopname)

    jdInfo=DataFrame([name,price,shopname,comment]).T
    jdInfo.columns=['产品名称','价格','商家名称','评论数']
    jdInfoAll=pd.concat([jdInfoAll,jdInfo])
jdInfoAll.to_excel('jdInfoAll.xls')
  • 写回答

2条回答 默认 最新

  • 人间再无张居正 2019-03-15 13:33
    关注

    由于我的版本是Python3.6,从lxml导入etree的方法有所改变,所以我下面的代码和你有些不同,你可以根据自己的环境进行修改,相信你也看的懂

    import requests
    # from lxml import etree
    from lxml import html
    from pandas import DataFrame
    import pandas as pd
    import re
    jdInfoAll=DataFrame()
    etree = html.etree
    for i in range(1,4):
        url="https://search.jd.com/Search?keyword=bosch&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&suggest=1.his.0.0&ev=exbrand_%E5%8D%9A%E4%B8%96%EF%BC%88BOSCH%EF%BC%89%5E&page="+str(i)
        headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
        res=requests.get(url, headers=headers, allow_redirects=False)
        res.encoding='utf-8'
        root=etree.HTML(res.text)
        name=root.xpath('//*[@id="J_goodsList"]/ul/li[@class="gl-item"]/div/div[@class="p-name p-name-type-2"]/a/em/text()[2]')
        for i in range(0,len(name)):
            name[i]=re.sub('\s','',name[i])
            print(i)
    
        #sku
        sku=root.xpath('//*[@id="J_goodsList"]/ul/li/@data-sku')
        print(sku)
    
        #价格
        price=[]
        comment=[]
        for i in range(0, len(sku)):
            thissku=sku[i]
            priceurl="https://p.3.cn/prices/mgets?callback=jQuery6775278&skuids=J_"+str(thissku)
            pricedata=requests.get(priceurl, headers=headers, allow_redirects=False)
            pricepat='"p":"(.*?)"}'
            thisprice=re.compile(pricepat).findall(pricedata.text)
            price=price+thisprice
    
            commenturl="https://club.jd.com/comment/productCommentSummaries.action?my=pinglun&referenceIds="+str(thissku)
            commentdata=requests.get(commenturl, headers=headers, allow_redirects=False)
            commentpat='"CommentCount":(.*?),"'
            thiscomment=re.compile(commentpat).findall(commentdata.text)
            comment=comment+thiscomment
    
        #商家名称
        shopname=root.xpath('//*[@id="J_goodsList"]/ul/li[@class="gl-item"]/div/div[@class="p-shop"]/span/a/@title')
        print(shopname)
    
        jdInfo=DataFrame([name,price,shopname,comment]).T
        jdInfo.columns=['产品名称','价格','商家名称','评论数']
        jdInfoAll=pd.concat([jdInfoAll,jdInfo])
    jdInfoAll.to_excel('jdInfoAll.xls')
    
    

    运行结果展示

    图片说明

    图片说明

    如果可以希望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗