爬虫之爬取车票信息

使用charles抓包软件,抓取手机APP全国汽车票的数据并实现查找车票的功能

关于charles使用可以参考https://blog.csdn.net/smalCat/article/details/79817398

手机和电脑配置好charles后,打开APP全国汽车票软件即可在charles看到抓包的数据
爬虫之爬取车票信息_第1张图片
在APP在上搜索车票信息时,发现多出一个链接(这里面很可能就有我们想要的车票信息)爬虫之爬取车票信息_第2张图片爬虫之爬取车票信息_第3张图片
抓取看看(里面的数据是unicode编码格式,注意要用unicode_escape来解码)爬虫之爬取车票信息_第4张图片
果然这里面有所有的车票信息,之后我发现这个URL包含了出发地址,终点地址和时间的信息。
爬虫之爬取车票信息_第5张图片
这样我们就可以通过修改URL来准确的获取我们想要的车票信息达到基本查票功能,代码如下
这样的数据%E6%B7%B1%E5%9C%B3可以用urllib.parse.quote来解码

import requests
import urllib
from urllib import parse
import json
import random


year = input("请输入年份")
month = input("请输入月份")
day = input("请输入日")

FromCity = input("请输入出发城市")
urllib.parse.unquote(FromCity)

ToCity = input("请输入终点城市")
urllib.parse.unquote(ToCity)

url = "http://gateway.qiche.tieyou.com/index.php?toCity="+str(ToCity)+"&fromDate="+str(year)+"-"+str(month)+"-"+str(day)+"&fromCity="+str(FromCity)+"&isForcedDSearch=0&isForcedASearch=0&method=product.getBusList&v=1.0¶m=%2Fapi%2Fhome&reqTime=1577164432&partner=tieyou_bus&version=20170610&channel=12308_apple_ios&ref=tieyou.app&n_app_v=8.1.8&basic_params=%7B%22app%22%3A%22bus_12308%22%2C%22big_channel%22%3A%2212308%22%2C%22smallchannel%22%3A%2212308_apple_ios%22%2C%22operat_system%22%3A%22ios%22%2C%22big_client_type%22%3A%22app%22%2C%22client_version%22%3A%221.00.002%22%7D"

agent_list = [
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
    # IPod
    "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
    # IPAD
    "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
    "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
    # Android
    "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; HTC_Wildfire_A3333 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    # QQ浏览器 Android版本
    "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    # Android Opera Mobile
    "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
    # Android Pad Moto Xoom
    "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
    # BlackBerry
    "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
    # WebOS HP Touchpad
    "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
    # Nokia N97
    "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
    # Windows Phone Mango
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
    # UC浏览器
    "UCWEB7.0.2.37/28/999",
    "NOKIA5700/ UCWEB7.0.2.37/28/999",
    # UCOpenwave
    "Openwave/ UCWEB7.0.2.37/28/999",
    # UC Opera
    "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999"
    ]

head = {"user-agent":random.choice(agent_list)}
try:
    response = requests.get(url, headers=head,verify=False,timeout=10)
    #print(response.text)
    html = response.content.decode('unicode_escape')
    #print(html)
    jsdict = json.loads(html)
    ZY = jsdict["return"]
    #print(html)
    for i in ZY:
        #print(i)
        CX =i["busType"]
        time = i["fromTime"]
        QD = i["fromStationName"]
        ZD = i["toStationShow"]
        JG = i["fullPrice"]
        PS = i["showTicketInfo"]
        print("起点:%s\t终点:%s,时间:%s,价格:%s,票数:%s,车型:%s\n"%(QD,ZD,time,JG,PS,CX))
except:
        print("无票")

运行结果
爬虫之爬取车票信息_第6张图片
爬虫之爬取车票信息_第7张图片爬虫之爬取车票信息_第8张图片

你可能感兴趣的:(爬虫)