版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
版权
import requests
import json
header = {
"Connection":"keep-alive",
"Cookie":"__cfduid=d0ef8be6f3ee70cb9e1aa7ba13977101e1563766025; __utmc=88287641; __utmz=88287641.1563766038.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utma=88287641.1340674523.1563766038.1563766038.1563770306.2",
"Host":"www.soupm25.com",
"Upgrade-Insecure-Requests":"1",
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}
class RunMethod:
def post_main(self,url,data,header=None):
res = None
if header != None:
res = requests.post(url=url,data=data,headers=header,verify=False)
else:
res = requests.post(url=url,data=data,verify=False)
print(res.status_code)
return res.json()
def get_main(self,url,data=None,header=None):
res = None
if header != None:
res = requests.get(url=url,data=data,headers=header,verify=False)
else:
res = requests.get(url=url,data=data,verify=False)
return res.json()
def run_main(self,method,url,data=None,header=None):
res = None
if method == 'post':
res = self.post_main(url,data,header)
else:
res = self.get_main(url, data, header)
print(json.dumps(res,ensure_ascii=False,sort_keys=True,indent=2))
if __name__ == '__main__':
run_m = RunMethod()
run_m.run_main('get','http://www.soupm25.com/',header=header)
报错: File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py”, line 357, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)