搜尋
首頁後端開發Python教學手把手教你用Python查詢你的物流訊息

/1 前言/

#    我們常常會去查快遞的物流單號,可是這些物流單號是從哪裡來的呢?

    快遞鳥集合了多家快遞公司查詢接口,輸入對應快遞公司編碼和快遞單號就可以獲得對應的物流資訊很方便快速。

手把手教你用Python查詢你的物流訊息


#/2 專案目標/

    教導大家如何用Python程式來查詢自己的物流資訊。


#/3 專案準備/

軟體:PyCharm

#所需的函式庫:##urllib.request、json#

網站如下:

https://www.kuaidi100.com


/4 專案分析/

一、#如何取得真正的網址,拿相對應的介面?

1、chrome瀏覽器F12檢查,隨便輸入一個快遞單號,點選尋找。如圖:

手把手教你用Python查詢你的物流訊息


2、找到

query?type=shentong&postid=773036432685909&temp=0.0

這個檔案。找到Request URL:複製網址。 ########################3、分析網址:######
https://www.kuaidi100.com/query?type=shentong&postid=773036432685909&temp=0.03191355265516216&phone=

4、可以看到postid就是我们的快递单号,type就是各个快递公司的名字拼音的简称。那等下就可以定义这个字典去存放各个公司名字的简称拼接网址。

5、找到Preview=>>对应的data可以看到context就是物流信息。等下可以通过js解析获取对应的字段。

手把手教你用Python查詢你的物流訊息


/5 项目实现/

1、定义一个class类继承object。导入需要的库和网址。定义一个字典kd_dict存放快递公司简称。

import urllib.request
import json


kd_dict = {1: 'shentong', 2: 'youzhengguonei', 3: 'yuantong', 4: 'shunfeng', 5: 'yunda', 6: 'zhongtong'}


class YU(object):
     pass


2、建立查询框架。

  while True:
      print("输入要查询快递公司:")
      print("1.申通  2.EMS邮政  3.圆通 4.顺风 5.韵达 6.中通  0.退出\n ")


      choose = int(input("请选择您的快递公司:"))
      while choose not in range(0, 7):
          choose = int(input("抱歉暂不支持此公司请重新选择:"))
      if choose == 0:
          break
      kd_num = input("请输入快递单号:")


3、对js文件进行解析。

    url = "http://www.kuaidi100.com/query?type=%s&postid=%s" % (kd_dict[choose], kd_num)
    response = urllib.request.urlopen(url)
    html = response.read().decode('utf-8')
    target = json.loads(html)


4、判断status的是不是200,是200才可以正常访问 ,获取对应的data。

 if status == '200':
      data = target['data']
      # print(data)
      data_len = len(data)
      # print(data_len)
      # print("\n")


5、for循环遍历,获取对应的字段。

  for i in range(data_len):
        print("\n时间: " + data[i]['time'])
        print("状态: " + data[i]['context'] + "")
    print("\n感谢使用!\n")
    break
else:
    print("输入有误请重新输入!\n")


6、程序循环。

   while True:
      YU.Check()
      out = input("按任意数字退出(其他键继续).........")
      if out >= &#39;0&#39; and out <= &#39;7&#39;:
          break
      else:
          print("\n")
          continue


/6 效果展示/

1、點選綠色小三角運行,之後輸入快遞公司代表的編號。

手把手教你用Python查詢你的物流訊息


2、輸入你的快遞單號,之後在控制台上可以看到顯示結果,如下圖所示。

手把手教你用Python查詢你的物流訊息


/7小結/

1、不建議抓取太多數據,容易對伺服器造成負載,淺嚐輒止即可。
2、這篇文章就python爬取快遞100,介紹如何去拼接字串,以及清單如何進行類型的轉換。
3、透過文中Python程序,可以清楚的看到你的快遞的物流資訊。

以上是手把手教你用Python查詢你的物流訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:Go语言进阶学习。如有侵權,請聯絡admin@php.cn刪除
Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循環時:您什麼時候在Python中使用?循環時:您什麼時候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境