name取为空

来源:9-5 通过requests完成京东详情页数据的获取

山楂姑娘

2020-07-28

图片描述
name昨天还是能取到值的,今天再运行就一直取的都是空值,打印出来的html链接点进去是京东登录的界面,是不是因为这个原因啊?老师有没有什么反爬的方法吗?

写回答

2回答

bobby

2020-08-03

加上user-agent就行了

import json

import requests

from scrapy import Selector


def parse_good(good_id):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'}
    good_url = "https://item.jd.com/{}.html".format(good_id)
    html = requests.get(good_url, headers=headers).text
    sel = Selector(text=html)
    # 获取商品的名称
    name = sel.xpath("//div[@class='sku-name']/text()").extract()
    # 获取商品的价格
    price_url = "https://p.3.cn/prices/mgets?type=1&skuIds=J_{}&source=item-pc".format(good_id)
    price_text = requests.get(price_url).text.strip()
    price_list = json.loads(price_text)
    print(html)
    if price_list:
        price = float(price_list[0]["p"])

    # 获取商品的评价信息
    evaluate_url = "https://club.jd.com/comment/productPageComments.action?productId={}&score=0&sortType=5&page={}&pageSize=10&isShadowSku=0&fold=1".format(good_id,0)
    evaluate_json = json.loads(requests.get(evaluate_url).text)
    max_page = 0
    max_page = evaluate_json["maxPage"]
    statistics = evaluate_json["hotCommentTagStatistics"]
    summary = evaluate_json["productCommentSummary"]
    evaluates = evaluate_json["comments"]
    pass


if __name__ == "__main__":
    parse_good(100006842459)


0
1
山楂姑娘
非常感谢!
2020-08-03
共1条回复

bobby

2020-07-30

你把完整的代码贴一下 我运行试试  也有可能是遇到反爬了

0
1
山楂姑娘
import json import requests from scrapy import Selector def parse_good(good_id): good_url = "https://item.jd.com/{}.html".format(good_id) html = requests.get(good_url).text sel = Selector(text=html) # 获取商品的名称 name = sel.xpath("//div[@class='sku-name']/text()").extract() # 获取商品的价格 price_url = "https://p.3.cn/prices/mgets?type=1&skuIds=J_{}&source=item-pc".format(good_id) price_text = requests.get(price_url).text.strip() price_list = json.loads(price_text) if price_list: price = float(price_list[0]["p"]) # 获取商品的评价信息 evaluate_url = "https://club.jd.com/comment/productPageComments.action?productId={}&score=0&sortType=5&page={}&pageSize=10&isShadowSku=0&fold=1".format(good_id,0) evaluate_json = json.loads(requests.get(evaluate_url).text) max_page = 0 max_page = evaluate_json["maxPage"] statistics = evaluate_json["hotCommentTagStatistics"] summary = evaluate_json["productCommentSummary"] evaluates = evaluate_json["comments"] pass if __name__ == "__main__": parse_good(100006842459)
2020-07-31
共1条回复

Python爬虫工程师实战 大数据时代必备

慕课网严选精品教程,高质量内容+服务!

2377 学习 · 1158 问题

查看课程