京东秒杀爬取失败
为什么爬取会报出这个错误,请大佬指出错误raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)# -*- coding:utf-8 -*-
import requests
from urllib.parse import urlencode
from pyquery import PyQuery as pq
headers = {
'authority': 'api.m.jd.com',
'method': 'GET',
'path': '/api?appid=o2_channels&functionId=pcMiaoShaAreaList&client=pc&clientVersion=1.0.0&callback=pcMiaoShaAreaList&jsonp=pcMiaoShaAreaList&body=%7B%7D&_=1616493634850',
'scheme': 'https',
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'cookie': 'TrackID=1xqFc2zG8fzZ1DgwKjzbAIXwVgdxq8FbvVKVqpOKGunmnVJEYBI433iK3erNEOqqlcmLqavJbDvZr2JNhepHCNAGITbTvWpllfIw-nwF9K75IiMD_x_iqu26W6K8iLUQP; pinId=iJEuA68AAzuroBl9QC0OE7V9-x-f3wj7; unpl=V2_ZzNtbUFRSh1xD0YDLBFbDWJQR18SVBZHcgEUUCkaDFZhAREOclRCFnUUR1dnGlwUZAAZXEVcQhVFCEdkexhdBGYBEVhAVXMlRQtGZHopXAJnARpdRVVEFnAAQFR9HFoEZgMRXEVncxV9DHZUehhdBWEAFVpDVkIlPFQEDT9FDDViAxNeQlVDE3Y4R2R6KQprZwITXENWRhZxAAtUfBleDWcEEFpBUksTdQ5DUnoYXAZmBCJcclQ%3d; shshshfpb=iwzn640W8VGfTaBwT9diXwA%3D%3D; shshshfp=e61376a46a79972cf4294c5e3f30b834; shshshfpa=be283d73-f7de-8663-87fe-95e9027adae4-1609231016; __jdu=16139163624971747001206; __jda=122270672.16139163624971747001206.1613916362.1613916364.1616492719.2; __jdv=122270672|direct|-|none|-|1616492718785; __jdc=122270672; areaId=7; ipLoc-djd=7-412-46823-0; __jdb=122270672.4.16139163624971747001206|2.1616492719; 3AB9D23F7A4B3C9B=H4M7Z6JQXGHM7T6JJFVYTEZQOMQZPPJTAVQWEY7SLGMC3KRLRYSYY6WIMADV2ZHF63PG567ARBKBDFCG7QMLOLVU2I',
'pragma': 'no-cache',
'referer': 'https://miaosha.jd.com/',
'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-fetch-dest': 'script',
'sec-fetch-mode': 'no-cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
base_url = 'https://api.m.jd.com/api?'
def get_page():
parmes = {
'appid': 'o2_channels',
'functionId': 'pcMiaoShaAreaList',
'client': 'pc',
'clientVersion': '1.0.0',
'callback': 'pcMiaoShaAreaList',
'jsonp': 'pcMiaoShaAreaList',
'body': '{}',
'_': '1616493634850'
}
url = base_url + urlencode(parmes)
try:
response = requests.get(url=url,headers=headers)
if response.status_code == 200:
return response.json()
except requests.ConnectionError as e:
print('错误',e.args)
def parse_page(json):
if json:
items = json.get('pcMiaoShaAreaList')
for item in items:
item = item.get('miaoShaList')
miaosha = {}
miaosha['名字'] = pq(item.get('shortWname')).text()
miaosha['秒杀时间'] = item.get('startTimeShow')
miaosha['折扣'] = item.get('rate')
miaosha['秒杀后价格'] = item.get('miaoShaPrice')
miaosha['秒杀前价格'] = item.get('jdPrice')
yield miaosha
if __name__ == '__main__':
json = get_page()
results = parse_page(json)
for i in results:
print(i) print(response.text)看一下就知道了,返回的不是直接的json数据。 suchocolate 发表于 2021-3-27 17:28
print(response.text)看一下就知道了,返回的不是直接的json数据。
那这种怎么办呢?{:5_96:}
页:
[1]