【问题标题】:Why my python mechanize script cannot work for this specific site为什么我的 python mechanize 脚本不能用于这个特定的站点
【发布时间】:2011-10-02 12:40:57
【问题描述】:

我需要模拟输入物品名称并点击http://ccclub.cmbchina.com/ccclubnew/上的搜索按钮的过程。

如果我直接在 HTML 中进行检查,搜索表单的描述名称为“searchKey”

<span class="searchinput">
     <input type="text" name="searchKey" id="searchKey" maxlength="25">
</span>

下面是脚本:

import mechanize
import cookielib

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Want debugging messages?
br.set_debug_http(True)
br.set_debug_redirects(True)
br.set_debug_responses(True)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

br.open("http://ccclub.cmbchina.com/ccclubnew/")

我明白了

br.select_form('searchKey')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 524, in select_form
    raise FormNotFoundError("no form matching "+description)
FormNotFoundError: no form matching name 'searchKey'

并且 br.forms() 是空的。

我的问题是:为什么 mechanize 不能选择 html 中存在的表单?有什么可能的解决方案来处理这个问题?

谢谢

【问题讨论】:

    标签: python forms search mechanize


    【解决方案1】:

    名称为searchKey 的input 本身不是形式。一个表单带有&lt;form&gt; 标签,但老实说,这个搜索框似乎不是表单的一部分;您必须模拟设置输入文本并按下它。

    【讨论】:

    • 感谢您的回答。你能指定如何设置输入的文本,如果这种场景不是机械化,那么有什么潜在的工具可以挖掘?
    【解决方案2】:

    使用lxmlBeatifulSoup 怎么样?

    【讨论】:

    • 这个。两者都安装时也有帮助
    • 我快速查看了美汤,并没有看到任何互动功能。听起来soup的主要用途是解析页面。
    猜你喜欢
    • 2023-02-13
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多