无法使用mechanize python访问字段

时间:2013-06-09 13:49:14

标签: python mechanize-python web-scraping

我正在尝试使用Mechanize Python使用以下代码进入网站http://www.sj.se/travel/booksearchlocation.form?mode=normal&l=sv

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)

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')]

# Open site
r = br.open("http://www.sj.se/travel/booksearchlocation.form?mode=normal&l=sv")

使用

选择表单
br.select_form(name='travelQuery')

正常工作但在输入字段中选择名称为“travelQuery.departureLocationName”

br["travelQuery.departureLocationName"] = "Uppsala C"

我收到以下错误:

Traceback (most recent call last):
  File "/home/joel/workspace/test/test/__init__.py", line 53, in <module>
    br["travelQuery.departureLocationName"] = "Uppsala C"
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 2780, in __setitem__
    control = self.find_control(name)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 3101, in find_control
    return self._find_control(name, type, kind, id, label, predicate, nr)
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 3185, in _find_control
    raise ControlNotFoundError("no control matching "+description)
mechanize._form.ControlNotFoundError: no control matching name 'travelQuery.departureLocationName'

我确定此输入字段存在,因为我在查看网站的源代码时可以看到它。打印表格时,我得到以下结果:

<travelQuery POST http://www.sj.se/travel/booksearchlocation.form;jsessionid=ED17C7B9906560F4DFAC8FD3DBA6E258.p1?mode=normal&l=sv application/x-www-form-urlencoded
  <HiddenControl(method=) (readonly)>
  <HiddenControl(mode=) (readonly)>
  <IgnoreControl(<None>=<None>)>
  <IgnoreControl(<None>=<None>)>>

为什么我不能选择输入字段?

0 个答案:

没有答案