肯德基网页的爬取

import urllib.request
import urllib.parse
import json

def getHtml(url):
#伪装浏览器
headers = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; WOW64)’}
r = urllib.request.Request(url=url, headers=headers)
# 定义post请求的参数
addr = input(“请输入查询的城市:”)
start = input(“请输入查询的页码:”)
size = input(“请输入查询的数量:”)
formdata = {
“cname”:"",
“pid”:"",
“keyword”:addr,
“pageIndex”:start,
“pageSize”: size,
}
# 解析post参数
query_string = urllib.parse.urlencode(formdata).encode()
# 向服务器发送请求
request = urllib.request.urlopen(r,data=query_string)
# 从服务器上下载数据
html = request.read().decode()
#将json格式转换为字典格式
a = json.loads(html)[“Table1”]
dict = {}
for i in a:
name = i[“storeName”]
addr = i[“addressDetail”]
dict[name] = dict.get(name,addr)
#print(name)
#print(addr)
print(dict)

getHtml(“http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword”)

你可能感兴趣的:(python)