python爬虫案列02,百度翻译,KFC城市查询
百度翻译(有点潦草,懒得优化)
看运行结果:
过程:
F12,输入一个单词,找到带有翻译结果的数据包,查看请求方式,请求的url,payload
代码:
# 导入模块
import requests
import json
word = input("输入要翻译的单词:")
# 制定URL
url = 'https://fanyi.baidu.com/sug'
# UA伪装
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36)'
}
# post 参数处理
data = {
'kw': word
}
# 模拟请求发送
response = requests.post(url=url, data=data, headers=headers)
# 结果保存到一个空字典
dic = response.json()
# 输出结果
for word in dic['data']:
print("{}".format(word))
KFC城市查询:
import requests
name = input("输入城市:")
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
pageIndex = '1'
data = {
'cname': '',
'pid': '',
'keyword': name,
'pageIndex': pageIndex,
'pageSize': '10'
}
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
response = requests.post(url, data=data, headers=head)
print(response.text)