import json标签:__,text,案列,headers,json,格式,requests From: https://www.cnblogs.com/ylxb-1/p/17041614.html
import requests
if __name__ == '__main__':
#json格式爬取案列
#1、选取请求网址
url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
#2、UAA伪装
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
#3、请求参数
data={
'cname':'',
'pid':'',
'keyword':'南昌',
'pageIndex':'1',
'pageSize':'10'
}
#4、requests.post响应封装,看响应的格式,大部分是json或者text
response =requests.post(url=url,params=data,headers=headers)
#text格式进行解码,将字符串格式转为json格式,利用json.loads函数
response.encoding='utf-8'
dec=response.text
de=json.loads(dec)