这是主要代码,为了爬取评论个数,我又从商品界面爬取数据 但是爬取的数据还是空
#%%
import re
import requests
import pandas
import numpy
from requests_html import UserAgent
from lxml import etree
import time
import random
#京东上搜集信息,以安宫牛黄丸按照评论数进行排序
data_url = 'https://search.jd.com/Search?keyword=%E5%AE%89%E5%AE%AB%E7%89%9B%E9%BB%84%E4%B8%B8&psort=4&wq=%E5%AE%89%E5%AE%AB%E7%89%9B%E9%BB%84%E4%B8%B8&psort=4&page=1&s=1&click=0'
#第二页page=3&s=61
#设置随机请求头
ua = UserAgent().random
header = {'User-Agent':ua}
response = requests.get(data_url,headers=header)
response.encoding = 'utf-8'
if response.status_code == 200:
#解析html代码
html = etree.HTML(response.text)
div_all = html.xpath('//ul[@class="gl-warp clearfix"]')
for div in div_all:
retail_price = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-price"]//i/text()')[0]
print('零售价:',retail_price)
manufactor = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-name p-name-type-2"]//em/text()')[0]
print('生产厂家:',manufactor)
name = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-name p-name-type-2"]//em/font/text()')[0]
print('产品名称:',name)
norms = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-name p-name-type-2"]//em/text()')[1]
print('规格:',norms)
unit = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-name p-name-type-2"]//em/font/text()')[1]
print('单位:',unit)
number = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-commit"]/strong/a/text()')
print('评价数量:',number)
shop = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-shop"]//a/text()')[0]
print('店铺名称:',shop)
icons = div.xpath('.//div[@class="gl-i-wrap"]/div[@class="p-icons"]/i/text()')[0]
print('店铺情况:',icons)
data = div.xpath('.//div[@class="gl-i-wrap"]/div[3]/a/@href')[0]
#获取商品链接
pattern = '//item.jd.com/(.*)'
data = re.findall(pattern,data)
#将列表转化呈字符串
data = ''.join(data)
html_name_url = 'https://item.jkcsjd.com/'+data
response_name = requests.get(html_name_url,headers=header)
response_name.encoding = 'utf-8'
html_name = etree.HTML(response_name.text)
numbers = html_name.xpath('//li[class="current"]//@em')
print('评价数量:',numbers)
这是数据结果,求大神解答
零售价: 1690.00 生产厂家: 广誉远 产品名称: 安宫牛黄丸 规格: 3g*2 单位: 丸 评价数量: [] 店铺名称: 广誉远官方旗舰店 店铺情况: 自营 评价数量: []
这是第一个评价数量对应的网页源代码

这是第二个评价数量对应的网页源代码
