在 Mechanize 上调用浏览器方法时,我收到一个 parse() 错误:意外的关键字'transport_encoding'



我正在尝试使用机械化提交表单。但是我可以从机械化浏览器中获取任何网页信息。我不知道如何解决问题。请帮我...

这是我的代码和错误结果。在代码中,当调用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:Python27libsite-packagesmechanize_mechanize.py", line 561, in title   
    return self._factory.title
File "C:Python27libsite-packagesmechanize_html.py", line 254, in title
    self.root) if self.root is not None else None
File "C:Python27libsite-packagesmechanize_html.py", line 247, in root
    response, verify=False))
File "C:Python27libsite-packagesmechanize_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.2.5版本。此版本运行良好。

如果有人遇到了同样的问题,请尝试使用另一个版本。

0.2.5现在是一个稳定版本,(2017-04-29(http://wwwsearch.sourceforge.net/mechanize/download.html

最新更新