Python realizes express logistics information query

I hope that what you fight for will be as you wish in the end.

1. Analyze the webpage

Express 100 website can easily query the logistics information of express

Insert picture description here

Insert picture description here

Two, python code implementation

1. selenium crawler implements query

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import cv2 as cv


# 根据快递单号查询物流信息
def get_screenshot_and_info():
    chrome_driver = r'D:\python\pycharm2020\chromedriver.exe'  # chromedriver的路径
    options = webdriver.ChromeOptions()
    # 关闭左上方 Chrome 正受到自动测试软件的控制的提示
    options.add_experimental_option('useAutomationExtension', False)
    options.add_experimental_option("excludeSwitches", ['enable-automation'])
    # 开启浏览器对象
    browser = webdriver.Chrome(options=options, executable_path=chrome_driver)
    # 访问这个url
    browser.get('https://www.kuaidi100.com/')
    # 显示等待
    wait = WebDriverWait(browser, 5)
    wait.until(ec.presence_of_element_located((By.ID, 'menu-track')))
    # 窗口最大化
    browser.maximize_window()
    browser.find_element_by_name('postid').send_keys(nums)
    browser.find_element_by_id('query').click()
    time.sleep(1)
    browser.find_element_by_id('query').click()
    time.sleep(2)
    browser.execute_script("window.scrollBy(0, 488)")
    # 截图
    browser.get_screenshot_as_file(filename='info.png')   
    items = browser.find_elements_by_xpath('//table[@class="result-info"]/tbody/tr')
    print('物流信息查询结果如下:\n')
    for item in items:
        time_ = item.find_element_by_xpath('.//td[1]').text.replace('\n', ' ')
        contex = item.find_element_by_xpath('.//td[3]').text
        print(f'时间:{time_}')
        print(f'状态:{contex}\n')
    browser.quit()
    # 显示截图
    src = cv.imread(filename='info.png')
    src = cv.resize(src, None, fx=0.7, fy=0.7)
    cv.imshow('result', src)
    cv.waitKey(0)


if __name__ == '__main__':
    nums = input('请输入您的单号:')
    print('\n')
    get_screenshot_and_info()

The operation effect is as follows:
Insert picture description here

2. The requests crawler implements query

import requests
import json

def query_info(i, j):
    headers = {
    
    
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24',
        "Referer": "https://www.kuaidi100.com/"
    }
    url = f'https://www.kuaidi100.com/query?type={j}&postid={i}&temp=0.53162373256954096&phone='
    resp = requests.get(url, headers=headers)
    # print(resp.text)
    datas = json.loads(resp.text)['data']
    # print(datas)
    print('您的快递物流信息查询结果如下:\n')
    for item in datas:
        time_ = item['time']
        info = item['context']
        print(f'时间:{time_}')
        print(f'物流状态:{info}' + '\n')


if __name__ == '__main__':
    delivery_dic = {
    
    
        '圆通': 'yuantong', '申通': 'shentong', '中通': 'zhongtong', '百世快递': 'huitongkuaidi',
        '韵达': 'yunda', '顺丰': 'shunfeng', '天天快递': 'tiantian', '邮政': 'youzhengguonei',
        'EMS': 'ems', '京东': 'jd', '德邦快递': 'debangwuliu', '极兔快递': 'jtexpress'
    }
    post_id = input('请输入你要查询物流的快递单号:')
    delivery = input('请输入快递公司:')
    query_info(post_id, delivery_dic[delivery])

The results are as follows:

Insert picture description here

The requests crawler query speed is faster, but you need to enter the courier company to facilitate the construction of the interface url to request the query.

Guess you like

Origin blog.csdn.net/fyfugoyfa/article/details/108670926
Recommended