python爬虫实战-爬附近肯德基信息(ajax-post)

python爬虫实战-爬附近肯德基信息(ajax-post)

import urllib.request
import urllib.parse

post_url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
city=input('请输入你要查询的城市:')
page=int(input('请输入你要查询第几页:'))
size=int(input('请输入你要查询的个数:'))

# city='西安'
# page=2
# size=10

form_data={
	'cname': city,
	'pid': '',
	'pageIndex': page,
	'pageSize': size,
}

headers={
	'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}

request=urllib.request.Request(url=post_url,headers=headers)
form_data=urllib.parse.urlencode(form_data).encode()
response=urllib.request.urlopen(request,data=form_data)
print(response.read().decode())

你可能感兴趣的:(python实战)