Python Mechanize在使用网站htm时遇到问题

2024-05-19 01:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python模块mechanize在网页上填写表单,然后下载得到的html。网站如下:

“xxx”

但首先,我只想列出表格。我的代码如下:

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)   # ignore robots
br.set_handle_refresh(False)  # can sometimes hang without this

url = "xxx"
response = br.open(url)
a = response.read()      # the text of the page

for form in br.forms():
    print "Form name:", form.name
    print form

给出的错误列表相当大:

^{pr2}$

我的理解是html写得很糟糕。我在google等示例网站上尝试过上面的代码,运行良好。我怀疑车厢的返回与此有关,但我如何绕过这个问题?在


Tags: the代码brformfalseurl网站response
1条回答
网友
1楼 · 发布于 2024-05-19 01:35:32

显然如果我用

br = mechanize.Browser(factory=mechanize.RobustFactory()) 

而不是

^{pr2}$

它提供正确的输出:

In [18]: % run -i form_filler.py
Form name: qSearchForm
<qSearchForm GET xxx
  <TextControl(q=Search)>
  <SubmitControl(qSearchBtn=) (readonly)>>
Form name: form1
<form1 POST xxx
  <TextControl(name=)>
  <SelectControl(code=[*LER, Lerwick, Scotland, 29.9 / 358.8, ESK, Eskdalemuir, Scotland, 34.7 / 356.8, HAD, Hartland, England, 39.0 / 355.5, ABN, Abinger, England, 38.8 / 359.6, GRW, Greenwich, England, 38.5 / 0.0])>
  <TextControl(month=)>
  <TextControl(year=)>
  <SubmitControl(<None>=Submit Query) (readonly)>
  <IgnoreControl(<None>=<None>)>>

相关问题 更多 >

    热门问题