上海金山农民画村,综艺最爱宪 瑶瑶,永康市邮编
部分python代码分享:
import requests
import city
import json
import jsonpath
import re
city_list = city.jsons
tags_list = city.tag
def city_func(city_id):
try:
city = jsonpath.jsonpath(city_list, '$..sub[?(@.code=={})]'.format(int(city_id)))[0]["name"]
except:
city = jsonpath.jsonpath(city_list, '$[?(@.code=={})]'.format(int(city_id)))[0]["name"]
return city
def tags_func(tags_id):
tags_join = []
if tags_id:
for tags in tags_id:
t = jsonpath.jsonpath(tags_list,'$..spotfiltertags[?(@.id=={})]'.format(int(tags)))
tags_join.append(t[0]["title"])
return ('-'.join(tags_join))
def split_n(ags):
return re.sub('\n',' ',ags)
def request(page):
print('开始下载第%d页'%page)
url = 'https://app-api.chargerlink.com/spot/searchspot'
two_url = "https://app-api.chargerlink.com/spot/getspotdetail?spotid={d}"
head = {
"device": "client=android&cityname=%e5%8c%97%e4%ba%ac%e5%b8%82&citycode=110106&lng=116.32154281224254&device_id=8a261c9d60acebded7cd3706c92dd68e&ver=3.7.7&lat=39.895024107858724&network=wifi&os_version=19",
"appid": "20171010",
"timestamp": "1532342711477",
"signature": "36daaa33e7b0d5d29ac9c64a2ce6c4cf",
"forcecheck": "1",
"content-type": "application/x-www-form-urlencoded",
"content-length": "68",
"host": "app-api.chargerlink.com",
"connection": "keep-alive",
"user-agent": "okhttp/3.2.0"
}
data = {
"userfilter[operatetype]": 2,
"citycode": 110000,
"sort": 1,
"page": page,
"limit": 10,
}
response = requests.post(url,data=data,headers=head)
#获取数据
data = response.json()
for i in data['data']:
c = []
id = i['id']
name = i["name"] #充电桩名
phone = i["phone"] #手机号
num = i['quantity'] #有几个充电桩
city = city_func(i["provincecode"]) #城市
tags =tags_func(i["tags"].split(','))#标签
message = c + [id,name,phone,num,city,tags]
parse_info(two_url.format(d=id),message)
def parse_info(url,message):
#打开文件
with open('car.csv','a',encoding='utf-8')as c:
head = {
"device": "client=android&cityname=&citycode=&lng=116.32154281224254&device_id=8a261c9d60acebded7cd3706c92dd68e&ver=3.7.7&lat=39.895024107858724&network=wifi&os_version=19",
"token": "036c8e24266c9089db50899287a99e65dc3bf95f",
"appid": "20171010",
"timestamp": "1532357165598",
"signature": "734ecec249f86193d6e54449ec5e8ff6",
"forcecheck": "1",
"host": "app-api.chargerlink.com",
"connection": "keep-alive",
"user-agent": "okhttp/3.2.0",
}
#发起详情请求
res = requests.get(url,headers=head)
price = split_n(jsonpath.jsonpath(json.loads(res.text),'$..chargingfeedesc')[0]) #价钱
paytype = jsonpath.jsonpath(json.loads(res.text),'$..paytypedesc')[0] #支付方式
businesstime =split_n(jsonpath.jsonpath(json.loads(res.text),'$..businesstime')[0]) #营业时间
result = (message + [price,paytype,businesstime])
r = ','.join([str(i) for i in result])+',\n'
c.write(r)
def get_page():
url = 'https://app-api.chargerlink.com/spot/searchspot'
head = {
"device": "client=android&cityname=%e5%8c%97%e4%ba%ac%e5%b8%82&citycode=110106&lng=116.32154281224254&device_id=8a261c9d60acebded7cd3706c92dd68e&ver=3.7.7&lat=39.895024107858724&network=wifi&os_version=19",
"appid": "20171010",
"timestamp": "1532342711477",
"signature": "36daaa33e7b0d5d29ac9c64a2ce6c4cf",
"forcecheck": "1",
"content-type": "application/x-www-form-urlencoded",
"content-length": "68",
"host": "app-api.chargerlink.com",
"connection": "keep-alive",
"user-agent": "okhttp/3.2.0"
}
data = {
"userfilter[operatetype]": 2,
"citycode": 110000,
"sort": 1,
"page": 1,
"limit": 10,
}
response = requests.post(url, data=data, headers=head)
# 获取数据
data = response.json()
total = (data["pager"]["total"])
page_size = (data["pager"]["pagesize"])
totalpage = (data['pager']["totalpage"])
print('当前共有{total}个充电桩,每页展示{page_size}个,共{totalpage}页'.format(total=total,page_size=page_size,totalpage=totalpage))
if __name__ == '__main__':
get_page()
start = int(input("亲,请输入您要获取的开始页:"))
end = int(input("亲,请输入您要获取的结束页:"))
for i in range(start,end+1):
request(i)
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论