作者drmexro (絲瓜瓜瓜)
看板Python
標題[問題] 用迴圈request網站無法取得資料
時間Sun Jun 4 13:33:59 2017
前陣子剛開始試著用python撈公開資料庫的資料
程式碼如下
import requests
from bs4 import BeautifulSoup as soup
payload={
"accountingyear":"",
"item":"00",
"crop":"001",
"city":"00",
"btnSend":"(unable to decode value)"
}
accountingyear=["100","101"]
for y in accountingyear:
payload['accountingyear']=y
print(payload)
r=requests.post("
http://agr.afa.gov.tw/afa/pgcropcity.jsp",data=payload)
print(r)
soup=soup(r.text.encode('utf-8'),'html.parser')
print(soup)
原本預期會得到100年, 101年的資料
可是結果是
{'accountingyear': '100', 'item': '00', 'crop': '001', 'city': '00',
'btnSend': '(unable to decode value)'}
<Response [200]>
<html><head>......</html>
{'accountingyear': '101', 'item': '00', 'crop': '001', 'city': '00',
'btnSend': '(unable to decode value)'}
<Response [200]>
[]
結果變成只有得到100年的資料,101年的資料只有[]
想知道是要如何解決
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.244.85.66
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1496554442.A.CD1.html
→ neil987: btnsend是有數值的 請用decode看看是什麼 06/04 16:04
→ neil987: 然後 好玩的一點是 request得到的text是正常的 06/04 16:17
→ neil987: 是丟進去soup的地方壞掉了你可以試試看print r.text 06/04 16:17
→ neil987: 為什麼壞掉 看看你前面import soup 又用soup作為變數 06/04 16:23
→ neil987: 所以把soup這個名稱改掉 就會正常了 06/04 16:24
→ drmexro: 原來如此 居然是在這種地方錯誤…謝謝 06/04 18:01