【发布时间】:2019-09-22 10:20:23
【问题描述】:
我正在尝试用多个值填写https://www.cancer.duke.edu/Nomogram/firstlinechemotherapy.html 上的表格并获得结果。请注意,URL 在提交时不会更改。 (验证按钮)
我尝试使用 Mechanize 填写表格并使用 Beautifulsoup 提取结果。但是由于 URL 永远不会改变,所以我无法接受响应。
import urllib.request
from urllib.request import urlopen
from bs4 import BeautifulSoup as bsoup
import mechanize
#Fill form with mechanize
br = mechanize.Browser()
br.open("https://www.cancer.duke.edu/Nomogram/firstlinechemotherapy.html")
response = br.response()
mech=response.read()
br.select_form(id='myform')
br.form['alb']='7'
br.form['hemo']='17'
br.form['alkph']='5000'
br.form['psa']='5000'
br.submit()
#Extract Output
url = urllib.request.urlopen("https://www.cancer.duke.edu/Nomogram/firstlinechemotherapy.html")
content = url.read()
soup= bsoup(content,"html.parser")
riskValue=soup.find('div',{'id':'resultPanelRisk3'})
tableValue=riskValue.find('table')
trValue=tableValue.find_all('tr')[1]
LowValue=trValue.find('td',{'id':'Risk3Low'}).string
IntermediateValue=trValue.find('td',{'id':'Risk3Intermediate'}).string
HighValue=trValue.find('td',{'id':'Risk3High'}).string
在上述代码中,LowValue 的值为“*”,而上述表单值的预期 LowValue 为“是”。
【问题讨论】:
-
看起来您没有解析 br 对象的输出,机械?所以无论你的 br 输出是什么,在你的下一个代码块中你都没有使用它。据我所知,mechanize 和 urllib 不会自动与每个对话。还可以查看 requests 和 selenium,许多人在自动化浏览器或发送自定义标头方面拥有丰富的经验
-
有没有办法让 mechanize 和 beautifulsoup 互相交谈?
-
为什么不使用 RoboBrowser?
标签: python forms beautifulsoup mechanize