返回列表 上一主題 發帖

[發問] 關於網頁資料抓取問題

本帖最後由 c_c_lai 於 2017-2-9 19:59 編輯

回復 11# VBALearner
請教為何會無 select 屬性?

我輸入內容是否有誤?
  1. import requests
  2. import time
  3. from bs4 import BeautifulSoup
  4. 主提單號碼=['160-69306532','608-10318674']

  5. with requests.session() as r:
  6.     res = r.get('https://accs.tradevan.com.tw/accsw-bin/APACCS/userLoginAction.do?userid=GUEST&password=GUEST')
  7.     for code in 主提單號碼:
  8.         print('主提單號碼:',code, '\n')
  9.         payload = {'mawb_no':code,'查詢':'查詢'}
  10.         res = r.post("https://accs.tradevan.com.tw/accsw-bin/APACCS/clmMergeQueryAction.do", data=payload)
  11.         res.encoding = "big-5"
  12.         html = BeautifulSoup(res.text,"html.parser")
  13.         for tr in html.body.select('table')[4].select('tr')[1:]:  #請詳閱w3school裡有關HTML的Table標籤知識,就能知道這裡在做啥
  14.             data = []  #建立陣列
  15.             for td in tr.select('td'):
  16.                 data.append(td.text.strip())
  17.                 if td == tr.select('td')[len(tr.select('td'))-1]: #這裡有下一個POST所需的資訊 (payload),透過觀察網頁原始碼得知
  18.                     print('第二次POST的資料:',td,'\n')
  19.             print('摘要資訊:',data, '\n')   #第一次查詢得到的摘要資料
複製代碼

TOP

回復 15# VBALearner
加了 #,排除 Select,接下來是索引範圍了!

TOP

回復 17# VBALearner
太棒了!謝謝你!

TOP

回復 21# VBALearner
蠻不錯的詮釋!
謝謝囉!

TOP

        靜思自在 : 太陽光大、父母恩大、君子量大,小人氣大。
返回列表 上一主題