首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用mechanize打开网页时出错

尝试使用mechanize打开网页时出错
EN

Stack Overflow用户
提问于 2019-08-15 05:07:31
回答 1查看 392关注 0票数 1

稍后,我将尝试学习机械化来创建聊天记录机器人,因此我测试了一些基本代码

代码语言:javascript
复制
import mechanize as mek
import re

br = mek.Browser()
br.open("google.com")

但是,每当我运行它时,我都会得到这个错误。

代码语言:javascript
复制
Traceback (most recent call last):
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 262, in _mech_open
    url.get_full_url
AttributeError: 'str' object has no attribute 'get_full_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    br.open("google.com")
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 253, in open
    return self._mech_open(url_or_request, data, timeout=timeout)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 269, in _mech_open
    raise BrowserStateError("can't fetch relative reference: "
mechanize._mechanize.BrowserStateError: can't fetch relative reference: not viewing any document

我仔细检查了机械化页面上的文档,它似乎是一致的。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-15 05:14:11

您必须使用模式,否则mechanize会认为您正在尝试打开一个本地/相对路径(如错误所示)。

br.open("google.com")应为br.open("http://google.com")

然后,您将看到一个错误mechanize._response.httperror_seek_wrapper: HTTP Error 403: b'request disallowed by robots.txt',因为google.com不允许爬虫。这可以在open之前使用br.set_handle_robots(False)进行修复。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57502081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档