使用python获取百度快递物流

使用python获取百度快递物流

使用python获取百度快递物流_第1张图片

看了很多都是写的快递100的代码。没有百度的,只能自己动手写个百度的,先贴出代码执行成功

使用python获取百度快递物流_第2张图片

经过请求分析,获取到物流请求的URl(去掉了没必要的参数)

https://alayn.baidu.com/express/appdetail/get_detail?
query_from_srcid=51151---------------------------固定
tokenV2=-----------------------------------------?
nu=----------------------------------------------快递单号
com=---------------------------------------------快递公司

有个tokenV2值,未知是怎么获取的

后面发现在百度的html里头就已经包含了tokenV2值
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=02003390_43_hao_pg&wd=%E5%BF%AB%E9%80%92

拿到tokenV2值后,执行代码会发现登录失效,然后看了下请求信息,原来是缺少了cookie,但是直接从百度搜索拿cookie的话不是要搞验证码就是脏数据,后面又经过发现直接访问百度可以获取到这个cookie所需要的值
https://www.baidu.com/

``

直接贴代码


import json
from urllib.parse import urlencode

import requests
import random
import re

# 获取cookie
def curlBaidu():
    url = "https://www.baidu.com"

    resp = requests.get(url, headers=getHeader())

    with open("cookies", "w") as f:
        for c in resp.cookies:
            f.write(f"{c.name}={c.value}\n")

# 获取toenkV2值
def curlBaiduKuaidi() :
    url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=02003390_43_hao_pg&wd=%E5%BF%AB%E9%80%92";
    header = getHeader()

    resp = requests.get(url, headers=header,cookies=getCookie())

    match_obj = re.search(r'tokenV2=(.*?)"', resp.text)

    global tokenV2
    cookies = resp.cookies

    cookie = {}
    for c in cookies:
        cookie[c.name] = c.value


    if match_obj is None :
        return False;
    else:
        tokenV2 = match_obj.group(1)
        return {"tokenV2":tokenV2,"cookie":cookie}

# 获取物流信息
def getExpress(nu,com):
    url = "https://alayn.baidu.com/express/appdetail/get_detail?"

    params = {
        "query_from_srcid":51151,
        "tokenV2": tokenV2,
        "nu":nu,
        "com":com
    }
    url = url + urlencode(params)

    resp = requests.get(url, headers={"Host":"alayn.baidu.com"},cookies=getCookie())

    return resp.text

def getHeader() :
    header = {
        "Host":"www.baidu.com",
        "Referer": "https://www.baidu.com",
        "User-Agent": getUserAgent(),
    }
    return header

def getCookie():
    with open('cookies', 'r') as f:
        content = f.read()

    content = content.rstrip("\n")
    temp_list = content.split("\n")
    cookies = {}

    # 遍历列表
    for data in temp_list:
        key = data.split('=', 1)[0]  # (以'='切割,1为切割1次)
        value = data.split('=', 1)[1]
        cookies[key] = value
    return cookies

def getUserAgent() :
    userAgent = [
        "User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
        "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
        "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    ];

    return random.choice(userAgent)

# 简单版本获取百度物流
# 主要获取cookie和tokenV2值
if __name__ == '__main__':
    curlBaidu() # 获取cookie值,后续是可以做缓存,时效?
    curlBaiduKuaidi = curlBaiduKuaidi() #获取tokenV2值,有时效可做缓存,时效多少?
    nu = "" # 物流单号
    com = "" # 物流公司编号
    print(getExpress(nu,com))

你可能感兴趣的:(python,dubbo,开发语言)