Python的mechanize无法正确解析表单

0 投票
1 回答
869 浏览
提问于 2025-04-16 04:19

我正在尝试用Python的mechanize库提交一个表单,但它没有正确解析这个表单。其他四个表单都能正确解析,只有这个表单有问题。不过在Perl的www::mechanize中,这个表单是能正确解析的,但我还是想用Python。

有没有办法获取页面的HTML,然后编辑它,让mechanize根据获取的HTML来解析和提交这个表单呢?

1 个回答

2

如果还有其他人感兴趣的话,我在mechanize的常见问题解答中找到了答案。

另外,你也可以随意处理HTML(和头部信息):

browser = mechanize.Browser()
browser.open("http://example.com/")
html = browser.response().get_data().replace("<br/>", "<br />")
response = mechanize.make_response(
    html, [("Content-Type", "text/html")],
    "http://example.com/", 200, "OK")
browser.set_response(response)

撰写回答