使用python抓取App数据

作者:NicePython
链接:https://zhuanlan.zhihu.com/p/84278415
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值