我试图使用机械化提交表单。 但我可以从机械化浏览器获取任何网页信息。 我不知道如何解决问题.... 请帮帮我......
以下是我的代码和错误结果。 在代码中,当调用browser.title(),browser.select_form()时,我收到一个错误,其中包含解析错误,无法找到关键字' transport_encoding'
代码:
browser = mechanize.Browser()
cj = cookielib.LWPCookieJar()
browser.set_cookiejar(cj)
browser.set_handle_equiv(True)
browser.set_handle_redirect(True)
browser.set_handle_robots(False)
browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
browser.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.3"')]
response = browser.open(u'https://www.google.com/')
print(browser.title())
print(response.geturl())
print(response.info())
print(response.read())
browser.select_form(nr=0)
browser.select_form(name=u'Form1')
# for form in self.browser.forms():
# print (form)
错误讯息:
Traceback (most recent call last):
File "D:/Projects/DormFilter/DormCrawler/Connector.py", line 168, in <module>
print(browser.title())
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 561, in title
return self._factory.title
File "C:\Python27\lib\site-packages\mechanize\_html.py", line 254, in title
self.root) if self.root is not None else None
File "C:\Python27\lib\site-packages\mechanize\_html.py", line 247, in root
response, verify=False))
File "C:\Python27\lib\site-packages\mechanize\_html.py", line 149, in content_parser
namespaceHTMLElements=False)
TypeError: parse() got an unexpected keyword argument 'transport_encoding'
当我检查调试模式时, self._factory有一个对象,self._factory.global_form有一个对象 但他们发生错误。 我做了什么?
#[This code is an library code]
def title(self):
' Return title, or None if there is no title element in the document. '
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
return self._factory.title
def select_form(self, name=None, predicate=None, nr=None, **attrs):
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
if name is None and predicate is None and nr is None and not attrs:
raise ValueError("at least one argument must be supplied to specify form")
global_form = self._factory.global_form
if nr is None and name is None and predicate is not None and predicate(
global_form):
self.form = global_form
return
答案 0 :(得分:1)
我尝试应用机械化0.2.5版本。 这个版本效果很好。
如果有人遇到同样的问题,请尝试使用其他版本。
0.2.5现在是一个稳定的版本,(2017-04-29) http://wwwsearch.sourceforge.net/mechanize/download.html