2016-12-06 51 views
0

我正在編寫一個腳本來跟蹤DHL包。我試圖解析與美麗的湯響應對象,並得到以下類屬性的表:Python請求:Response.text屬性返回一個不是值的模板

<table class="result-checkpoints show result-has-pieces" summary="DHL Express shipments checkpoints"> 

然而,當我打印Response對象的文本屬性,它顯示了一個模板,像這樣:

<table class="result-checkpoints<%= results.length > 1 ? '' : ' show' %><%= hasPieces ? ' result-has-pieces' : '' %><%= hasEdd ? ' result-has-edd' : '' %>" summary="<%= messages.checkpoints.summary %>"> 

我想這就是爲什麼我的腳本沒有找到元素?如何獲得瀏覽器中顯示的響應?

這是腳本:

import requests 
from bs4 import BeautifulSoup 

class DHLTracker(): 
    def __init__(self): 
     self.session = requests.Session() 
     self.url = "http://www.dhl.com/en/express/tracking.html?AWB={}&brand=DHL" 
     self.response = None 
     self.status = None 

    def searchStatus(self, awb): 
     '''pass and awb an return the last status''' 
     session = self.session 
     url = self.url.format(awb) 
     response = session.get(url=url) 
     if response.status_code == 200: 
      self.response = response.text 
      self.parseResponse() 

    def parseResponse(self): 
     soup = BeautifulSoup(self.response, "html.parser") 
     table = soup.find_all(name="table", attrs={"class":"result-checkpoints show result-has-pieces"}) 
     print table 
     #self.status = table[0].tbody.tr[0].td[2] 
     return 


if __name__ == '__main__': 
    dhl = DHLTracker() 
    dhl.searchStatus("4364282856") 
    print dhl.status 

回答

2

此頁面使用JavaScript來獲取數據,原始數據是在這個url

http://www.dhl.com/shipmentTracking?AWB=4364282856&countryCode=g0&languageCode=en&_=1481000240265 

你可以改變這個網址的AWB獲取所有數據

相關問題