采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
name昨天还是能取到值的,今天再运行就一直取的都是空值,打印出来的html链接点进去是京东登录的界面,是不是因为这个原因啊?老师有没有什么反爬的方法吗?
加上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)
非常感谢!
你把完整的代码贴一下 我运行试试 也有可能是遇到反爬了
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)
登录后可查看更多问答,登录/注册
慕课网严选精品教程,高质量内容+服务!
1.6k 20
2.0k 17
1.5k 17
1.4k 17
1.8k 13
购课补贴联系客服咨询优惠详情
慕课网APP您的移动学习伙伴
扫描二维码关注慕课网微信公众号