preamble
Recently, the domestic epidemic situation has improved, the courier industry is gradually restored, everyone's express is not running up? This article to explain how to let python automatically for you to query the courier information, and in the logistics update or arrive at the designated location will be the first time the logistics pushed to your mailbox!
It's not really complicated, and it only takes two steps to get it right:
- Crawling logistics dynamic information
- Push messages to email
Logistics Tracking
First of all said the pit, at the beginning of the idea is to find a courier query site to check the courier, and then grab a package to get the content we need, and then write a crawler to track the courier information, the results of a Baidu found that the basic domestic check the courier site with the courier 100 API, apply for an API to provide a website for each other to review, and there are restrictions on the number of times. Do not apply for the API to climb directly to get the logistics information is all kinds of encryption. GitHub also did not find a GitHub can be used. Tossing half a day finally found a website can be used. Directly on the code
import requests import bs4 import re kuaidi = [] url='http://m./express/?typetxt=%D6%D0%CD%A8&type=zto&number=your single number' response = (url) = 'gb18030' response = soup = (response,'',from_encoding="utf8") for i in (name='div',attrs = {'class':'icontent'}): (i.get_text()) print(i.get_text())
This piece of code we use more than many times but not too much interpretation, with requests + bs4 crawl results and extract the target information we need, of course, note that this is just a link to the query of China Express
So we need to write another function to generate more courier company lookup links, which isn't very difficult either
def express_type_get(): express_type = ('sfexpress','yunda','sto','yto','zto','ems','ttdex','htky','qfkd','chinapost') print('//////////////// courier company ////////////////\n1.shunfeng 2.yunda 3.shentong 4.yuntong 5.zhongtong\ 7.tiandian 8.huitong 9.all peak 10.postal\n////////////////////////////////////////') while True: express = int(input('Please select a courier(numeric):')) if express: if express <= 10 and express >= 1: break else: print("Wrong choice!") else: print("Can't be empty!") return express_type[express-1] def get_url(code,id): url = 'http://m./express/?typetxt=%D6%D0%CD%A8&type=' + code + '&number=' + id return url
This way we have completed the first step! Finally, we save all the logistic information in a list
Logistics information pushed to e-mail
In the previous step we got the latest logistics information of the courier, so if we want to send the latest logistics information to the mailbox how do we do it? It's not difficult, just use the yagmail module, which is very easy to install. pip install yagmail
Directly look at the code, to qq mailbox for example
def send(kuaidi): yag = ( user="Login mailbox", password="Password.", host='') # Mailbox body contents = kuaidi # Send mail ('Target Mailbox', 'Latest Express Tracking', contents) print('Email sent successfully')
The first line to log in to the mailbox, the second line to fill in the content of the message, the third line to send the message, white can understand, but it should be noted that the mailbox password here is not your QQ mailbox login password but the SMTP server password, you can follow the tips below to get
In this way, as long as we write a command line script to query your courier logistics information sent to the mailbox regularly or let the script query the courier once an hour.
# Main program (3600)
Or more frequently to check logistics information and in the event of logistics changes or express delivery to the designated location to send e-mail!
# If the courier arrives in Hangzhou send a message to the e-mail address if 'Hangzhou' in kuaidi[0]: send()
Or any other courier dynamics you care about can be customized. Of course, keep in mind that you can only use it for your own delivery checks and be careful about the frequency of requests so as not to affect other people's services.
summarize
to this article on the use of python automatically track your courier article is introduced to this, more related python automatically track the contents of the courier please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!