gpt4 book ai didi

python - 尝试使用 Mechanize 打开网页时出错

转载 作者:行者123 更新时间:2023-12-04 16:18:57 25 4
gpt4 key购买 nike

我正在尝试学习 Mechanize 以稍后创建聊天记录机器人,因此我测试了一些基本代码

import mechanize as mek
import re

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

但是,每当我运行它时,都会出现此错误。
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

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

最佳答案

您必须使用架构,否则 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不允许爬虫。这可以通过 br.set_handle_robots(False) 解决之前 open .

关于python - 尝试使用 Mechanize 打开网页时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57502081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com