requests之肯德基座位爬取
import requests
import json
if __name__ =='__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
} # 1进行UA伪装,指定url
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
kw = input("enter a word : ")
data = {
'cname': '北京',
'pid': '',
'keyword':kw,
'pageIndex': '1',
'pageSize': '10'
}
res = requests.post(url=url,data=data,headers= headers) # 2发起请求
dic_obj = res.text # 3获取响应数据
print(dic_obj)
fp = open('./kfc.json','w',encoding='utf-8') # 4 持久化存储数据
json.dump(dic_obj,fp,ensure_ascii=False)
print('爬取成功!!')
作者:华王
博客:https://www.cnblogs.com/huahuawang/