【发布时间】:2016-05-04 09:36:41
【问题描述】:
我花了一天半的时间(我是初学者)试图弄清楚如何做一些简单的事情:填写 ID 代码并点击“搜索”。我已经阅读了请求文档并搜索了堆栈溢出示例,但似乎没有任何帮助。
我想要做的是遍历我拥有的 ID 列表并检查哪些是有效的,哪些是无效的。 (这是为了学术研究) 这是我的代码:
import requests
url = 'https://ndber.seai.ie/pass/ber/search.aspx'
payload = {'ctl00$DefaultContent$BERSearch$dfSearch$txtBERNumber':'100000000'
}
#the above dictionary key corresponds to the name , found using Firefox inspector in the source code.
#I have tried using input ID too with no difference in outcome
#enter in arbitrary number of '100000000'- expect to see 'Invalid ID Number' printed
r = requests.post(url, data = payload)
print(r.status_code, r.reason)
print(r.text)
if 'Number.'in r.text: #'Number' is an arbitrary word that happens to be in the page if a correct ID code is entered
print('Valid ID Number')
elif 'No results found.' in r.text: #as above, 'No results found' is in the page if an incorrect ID is entered
print('Invalid ID Number') #with the aboove number entered, I would expect this line to be printed
else:
print('Code Failure') #this line gets printed
我已尝试尽可能多地对其发表评论,以便您了解我的意思。
我得到的输出是Code Failure
BeautifulSoup 之所以出现这一切,是因为我昨天在 Reddit 的优秀“learn python”subreddit 上寻求帮助。一位 Redditor 慷慨地花时间试图解释我哪里出错了,以及我应该做些什么不同的事情。他建议我这样做: enter image description here,涉及BS4。
那么,Redditor 是对的,还是可以使用 requests 库以简单且“轻松”的方式完成?
干杯!
【问题讨论】:
-
如果有人能解释为什么我没有得到任何回应,我将不胜感激。问题是否以某种方式格式不正确?我是否包含太多/太少的信息?是不是太长了?
标签: python-3.x beautifulsoup python-requests