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

山楂姑娘
2020-07-28
name昨天还是能取到值的,今天再运行就一直取的都是空值,打印出来的html链接点进去是京东登录的界面,是不是因为这个原因啊?老师有没有什么反爬的方法吗?
写回答
2回答
-
加上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)
012020-08-03 -
bobby
2020-07-30
你把完整的代码贴一下 我运行试试 也有可能是遇到反爬了
012020-07-31
相似问题
老师获取不到cookie值
回答 1
获取Name取不到值
回答 1