Skip to main content
 首页 » 编程设计

python之如何使用 twill 查看当前 url

2025年05月04日798°冷暖

我打算使用 twill 在一页上填写表单,点击提交按钮,然后使用 BeautifulSoup 解析生成的页面。如何向 BeautifulSoup 提供 HTML 页面?我假设我必须阅读当前的网址,但我不知道如何实际返回网址才能这样做。我试过 twill 的 TwillBrowser.get_url(),但它只返回 None。

请您参考如下方法:

对于任何 future 的患者,我发现使用 mechanize 而不是 twill 的运气更好,因为 twill 是用于 mechanize 的未更新的薄外壳。解决方法如下:

import mechanize 
 
url = "foo.com" 
br = mechanize.Browser() 
 
br.open(url) 
 
br.select_form(name = "YOURFORMNAMEHERE") #make sure to leave the quotation marks 
br["YOURINPUTFIELDNAMEHERE"] = ["YOURVALUEHERE"] #this must be in a list even if it is only one value 
 
response = br.submit() 
 
print response.geturl()