[适合新手]爬取肯德基线下门店地址并进行存储
import requestsimport csv
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}
f = open('book.csv',mode='a',encoding='utf-8',newline='')
#表头
csv_writer = csv.DictWriter(f,fieldnames=[
'餐厅名称',
'餐厅地址',
'详情',
])
#表单数据
for i in range(1,11):
data = {
'cname': '',
'pid': '',
'keyword': '北京',
'pageIndex': f'{i}',
'pageSize': '10',
}
#请求字符串
params = {'op':'keyword'}
response = requests.post(url=url, params=params, headers=headers, data=data)
json_data = response.json() #将结果进行反序列化 将对象转化为可以传输、储存的数据
for i in json_data['Table1']:
name = i['storeName']
address = i['addressDetail']
services = i['pro']
print(name,address,services)
dict = {
'餐厅名称':name+'餐厅',
'餐厅地址':address,
'详情':services
}
csv_writer.writerow(dict) import requests
import csv
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}
def page_number(names):
data = {
'cname':'',
'pid':'',
'keyword':names,
'pageIndex':'1',
'pageSize':'10',
}
response = requests.post(url=url,headers=headers,data=data)
json_data = response.json()
number = json_data['Table']['rowcount']
return number
def parse_json(name):
number1 = page_number(name)
for i in range(1,number1+1):
data = {
'cname': '',
'pid': '',
'keyword': name,
'pageIndex': str(i),
'pageSize': '10',
}
response = requests.post(url=url,headers=headers,data=data)
json_data = response.json()
for p in json_data['Table1']:
canteen = p['storeName']
address = p['addressDetail']
pro = p['pro']
print(canteen,address,pro)
parse_json('大连') 程序猿高手失敬失敬,未来靠你们了 可惜看不懂~~ 今天爬地址,明天疯四就免费 liuchunjie 发表于 2022-8-20 06:18
今天爬地址,明天疯四就免费
人才,哈哈哈哈哈 支持楼主 一般我都是手机一按,半小时就送到,不用出门! tl;dr 发表于 2022-8-20 06:05
为啥做这个呢?
刚入门找些例子练练手。。 这个好像我在哪个教程看到过{:1_918:}
页:
[1]
2