[问题] 用循环request网站无法取得资料

楼主: drmexro (丝瓜瓜瓜)   2017-06-04 13:33:59
前阵子刚开始试着用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年的资料只有[]
想知道是要如何解决
谢谢
作者: neil987 (R5大小姐-EX人品崩坏)   2017-06-04 16:04:00
btnsend是有数值的 请用decode看看是什么说错=>encode... http://i.imgur.com/3Qyour0.png然后 好玩的一点是 request得到的text是正常的是丢进去soup的地方坏掉了你可以试试看print r.text为什么坏掉 看看你前面import soup 又用soup作为变量所以把soup这个名称改掉 就会正常了
楼主: drmexro (丝瓜瓜瓜)   2017-06-04 18:01:00
原来如此 居然是在这种地方错误…谢谢

Links booklink

Contact Us: admin [ a t ] ucptt.com