为什么我收到“ERR_ACCESS_DENIED”当我使用 python 打开 URL(维基百科)时?
这是我在这里发布的第一个问题,所以我可能会为新手寻找一点遗憾。
我在高中的计算机科学课上学习 Python,所以我在使用它来解决问题方面几乎没有经验。现在,我正在研究通过跟踪每个页面上的链接从维基百科中的随机页面到目标页面(也在维基中)的内容。这是我第一次使用像 urllib 这样的东西,所以到目前为止我只使用老师告诉我使用的东西。
我有一些代码应该能够打开 Wiki 中的页面,但我不断地想出一个页面,其中提到了一些技术错误。不过,从浏览器打开维基百科是可以的。
我不知道需要什么才能让它发挥作用,而且我也不知道到哪里去解决这个问题。
我的代码(在 Ubuntu 11.04 中使用 IDLE 使用 Python 2.7):
import urllib
import HTMLParser
class Parser(HTMLParser.HTMLParser):
def handle_starttag(self, tag, attrs):
print("Start:", tag, attrs)
def handle_endtag(self, tag):
print("End:", tag)
def handle_data(self, data):
print ("Data:", data)
#proxy = {"http": "http://10.102.0.3:3128"}
browser = urllib.FancyURLopener()#(proxies = proxy)
#The commented-out stuff is for the proxy at school.
#Both at home and at school, I come up with errors.
f = browser.open("http://en.wikipedia.org/wiki/Special:Random")
p = Parser()
print p.feed(f.read())
我的输出如下所示:(
'Data:', '\n') ('开始:', 'html', [('xmlns', 'http://www.w3.org/1999/xhtml'), ('xml:lang', 'en'), ('lang', '恩')]) ('数据:', '\n') ('开始:', '头', []) ('数据:', '\n') ('开始:', '标题', []) ('数据:','维基媒体错误') ('结束:','标题') ('数据:', '\n') ('开始:', '元', [('http-equiv', '内容类型'), ('内容', 'text/html; charset=UTF-8')]) ('结束:','元') ('数据:', '\n') ('开始:', '元', [('姓名', '作者'), ('内容', '马克·瑞安')]) ('结束:','元') ('数据:', '\n') ('Start:', 'meta', [('name', 'copyright'), ('content', '(c) 2005-2007 Mark Ryan 等人。根据 GNU 自由文档许可证获得许可的文本。http://www.gnu.org/licenses/fdl.txt')]) ('结束:','元') ('数据:', '\n\n') ('开始:', '样式', [('类型', '文本/css')]) ('数据:', '\n') ('结束:', '风格') ('数据:', '\n') ('开始:', '脚本', []) ('Data:', '//\n\t函数行 {\n\t\tvar c = s.split(\' \');\n\t\tfor (var i = 0; i < ; c.length; i++) {\n\t\tdocument.write(\'') ('结束:', 'div') ('数据:', "');\n\t\t}\n\t}\n//]]>") ('结束:','脚本') ('数据:', '\n') ('结束:','头') ('数据:', '\n\n') ('开始:', 'body', [('link', '#24442E'), ('text', '#000000'), ('vlink', '#24442E'), ('alink', ' #FF0000')]) ('数据:', '\n') ('开始:', 'h1', []) (“数据:”,“维基媒体基金会”) ('结束:','h1') ('数据:', '\n') ('开始:', '脚本', []) ('数据:', "行('ccd4cf bdc3bf adb1af 9ea09f dbe5df');") ('结束:','脚本') ('数据:', '\n\n') ('开始:', 'h2', []) ('数据:','错误') ('结束:','h2') ('数据:', '\n\n') ('开始:', '脚本', []) ('数据:', "行('8f8f8f acacac c6c6c6 dbdbdb eaeaea f4f4f4');") ('结束:','脚本') ('数据:', '\n\n') ('数据:', '\n') ('开始:', 'div', [('class', 'ContentArea')]) ('数据:', '\n\n') ('开始:', 'div', [('id', 'en'), ('lang', 'en')]) ('数据:', '\n') ('开始:', 'p', []) ('数据:','我们的服务器当前遇到技术问题。这可能是暂时的,应该很快修复。请') ('开始:', 'a', [('href', 'http://en.wikipedia.org/wiki/Special:Random'), ('onclick', 'window.location.reload(false);返回 false')]) ('数据:','再试一次') ('结束:', 'a') (“数据:”,“几分钟后。”) ('结束:', 'p') ('数据:', '\n') ('开始:', 'p', []) ('数据:', '您也许能够在中获得更多信息') ('开始:', 'a', [('href', 'irc://chat.freenode.net/wikipedia')]) ('数据:','#wikipedia') ('结束:', 'a') ('数据:','通道上') ('开始:', 'a', [('href', 'http://www.freenode.net')]) ('数据:', 'Freenode IRC 网络') ('结束:', 'a') ('数据:', '。') ('结束:', 'p') ('数据:', '\n') ('开始:', 'p', []) (“数据:”,“维基媒体基金会是一个非营利组织,托管着互联网上一些最受欢迎的网站,包括维基百科。它不断需要购买新硬件。如果您愿意提供帮助,请” ) ('开始:', 'a', [('href', 'http://wikimediafoundation.org/wiki/Fundraise')]) ('数据:','捐赠') ('结束:', 'a') ('数据:', '。') ('结束:', 'p') ('数据:', '\n') ('开始:', 'hr', [('noshade', 'noshade'), ('尺寸', '1px'), ('宽度', '80%')]) ('结束:','小时') ('数据:', '\n') ('开始:', 'div', [('class', 'TechnicalStuff')]) ('数据:', '\n如果您向维基媒体系统管理员报告此错误,请提供以下详细信息。') ('开始:', 'br', []) ('结束:', 'br') ('数据:', '\n') ('结束:', 'div') ('数据:', '\n') ('开始:', 'div', [('class', 'TechnicalStuff')]) ('数据:', '\n') ('开始:', 'bdo', [('dir', 'ltr')]) ('数据:', '\n请求: GET http://en.wikipedia.org/wiki/特别:随机,从 112.205.80.8 通过 sq72.wikimedia.org (squid/2.7.STABLE9) 到 ()') ('开始:', 'br', []) ('结束:', 'br') ('数据:', '\n错误: ERR_ACCESS_DENIED, errno [无错误] 于 2012 年 2 月 6 日星期一 11:58:50 GMT\n') ('结束:', 'bdo') ('数据:', '\n') ('结束:', 'div') ('数据:', '\n') ('结束:', 'div') ('数据:', '\n\n') ('结束:', 'div') ('数据:', '\n') ('开始:', '脚本', []) ('数据:', "行('9ea09f adb1af bdc3bf ccd4cf');") ('结束:','脚本') ('数据:', '\n\n') ('结束:','正文') ('数据:', '\n') ('结束:', 'html') ('数据:', '\n\n') 没有任何
this is my first ever question posted here, ever, so I might be looking for a little pity for a newbie.
I'm learning Python for my Computer Science class in high school, so I have little experience in using it to solve problems. Right now I'm working on something that gets from a random page in Wikipedia to a targeted page (also in Wiki) by following the links on each page. This is my first time to use such stuff such as urllib, so I'm only using so far what my teacher told me to use.
I've got a bit of code that should be able to open up pages in the Wiki, but I keep coming up with a page that says something about a technical error. Opening Wikipedia from a browser is alright, though.
I don't know what I need to get it to work, and I have no idea anymore on where to look to figure this out.
My code (using IDLE in Ubuntu 11.04 using Python 2.7):
import urllib
import HTMLParser
class Parser(HTMLParser.HTMLParser):
def handle_starttag(self, tag, attrs):
print("Start:", tag, attrs)
def handle_endtag(self, tag):
print("End:", tag)
def handle_data(self, data):
print ("Data:", data)
#proxy = {"http": "http://10.102.0.3:3128"}
browser = urllib.FancyURLopener()#(proxies = proxy)
#The commented-out stuff is for the proxy at school.
#Both at home and at school, I come up with errors.
f = browser.open("http://en.wikipedia.org/wiki/Special:Random")
p = Parser()
print p.feed(f.read())
My output looks like this:
('Data:', '\n')
('Start:', 'html', [('xmlns', 'http://www.w3.org/1999/xhtml'), ('xml:lang', 'en'), ('lang', 'en')])
('Data:', '\n')
('Start:', 'head', [])
('Data:', '\n')
('Start:', 'title', [])
('Data:', 'Wikimedia Error')
('End:', 'title')
('Data:', '\n')
('Start:', 'meta', [('http-equiv', 'Content-Type'), ('content', 'text/html; charset=UTF-8')])
('End:', 'meta')
('Data:', '\n')
('Start:', 'meta', [('name', 'author'), ('content', 'Mark Ryan')])
('End:', 'meta')
('Data:', '\n')
('Start:', 'meta', [('name', 'copyright'), ('content', '(c) 2005-2007 Mark Ryan and others. Text licensed under the GNU Free Documentation License. http://www.gnu.org/licenses/fdl.txt')])
('End:', 'meta')
('Data:', '\n\n')
('Start:', 'style', [('type', 'text/css')])
('Data:', '\n')
('End:', 'style')
('Data:', '\n')
('Start:', 'script', [])
('Data:', '//\n\tfunction lines(s) {\n\t\tvar c = s.split(\' \');\n\t\tfor (var i = 0; i < c.length; i++) {\n\t\t\tdocument.write(\'')
('End:', 'div')
('Data:', "');\n\t\t}\n\t}\n//]]>")
('End:', 'script')
('Data:', '\n')
('End:', 'head')
('Data:', '\n\n')
('Start:', 'body', [('link', '#24442E'), ('text', '#000000'), ('vlink', '#24442E'), ('alink', '#FF0000')])
('Data:', '\n')
('Start:', 'h1', [])
('Data:', 'Wikimedia Foundation')
('End:', 'h1')
('Data:', '\n')
('Start:', 'script', [])
('Data:', "lines('ccd4cf bdc3bf adb1af 9ea09f dbe5df');")
('End:', 'script')
('Data:', '\n\n')
('Start:', 'h2', [])
('Data:', 'Error')
('End:', 'h2')
('Data:', '\n\n')
('Start:', 'script', [])
('Data:', "lines('8f8f8f acacac c6c6c6 dbdbdb eaeaea f4f4f4');")
('End:', 'script')
('Data:', '\n\n')
('Data:', '\n')
('Start:', 'div', [('class', 'ContentArea')])
('Data:', '\n\n')
('Start:', 'div', [('id', 'en'), ('lang', 'en')])
('Data:', '\n')
('Start:', 'p', [])
('Data:', 'Our servers are currently experiencing a technical problem. This is probably temporary and should be fixed soon. Please ')
('Start:', 'a', [('href', 'http://en.wikipedia.org/wiki/Special:Random'), ('onclick', 'window.location.reload(false); return false')])
('Data:', 'try again')
('End:', 'a')
('Data:', ' in a few minutes.')
('End:', 'p')
('Data:', '\n')
('Start:', 'p', [])
('Data:', 'You may be able to get further information in the ')
('Start:', 'a', [('href', 'irc://chat.freenode.net/wikipedia')])
('Data:', '#wikipedia')
('End:', 'a')
('Data:', ' channel on the ')
('Start:', 'a', [('href', 'http://www.freenode.net')])
('Data:', 'Freenode IRC network')
('End:', 'a')
('Data:', '.')
('End:', 'p')
('Data:', '\n')
('Start:', 'p', [])
('Data:', 'The Wikimedia Foundation is a non-profit organisation which hosts some of the most popular sites on the Internet, including Wikipedia. It has a constant need to purchase new hardware. If you would like to help, please ')
('Start:', 'a', [('href', 'http://wikimediafoundation.org/wiki/Fundraising')])
('Data:', 'donate')
('End:', 'a')
('Data:', '.')
('End:', 'p')
('Data:', '\n')
('Start:', 'hr', [('noshade', 'noshade'), ('size', '1px'), ('width', '80%')])
('End:', 'hr')
('Data:', '\n')
('Start:', 'div', [('class', 'TechnicalStuff')])
('Data:', '\nIf you report this error to the Wikimedia System Administrators, please include the details below.')
('Start:', 'br', [])
('End:', 'br')
('Data:', '\n')
('End:', 'div')
('Data:', '\n')
('Start:', 'div', [('class', 'TechnicalStuff')])
('Data:', '\n')
('Start:', 'bdo', [('dir', 'ltr')])
('Data:', '\nRequest: GET http://en.wikipedia.org/wiki/Special:Random, from 112.205.80.8 via sq72.wikimedia.org (squid/2.7.STABLE9) to ()')
('Start:', 'br', [])
('End:', 'br')
('Data:', '\nError: ERR_ACCESS_DENIED, errno [No Error] at Mon, 06 Feb 2012 11:58:50 GMT\n')
('End:', 'bdo')
('Data:', '\n')
('End:', 'div')
('Data:', '\n')
('End:', 'div')
('Data:', '\n\n')
('End:', 'div')
('Data:', '\n')
('Start:', 'script', [])
('Data:', "lines('9ea09f adb1af bdc3bf ccd4cf');")
('End:', 'script')
('Data:', '\n\n')
('End:', 'body')
('Data:', '\n')
('End:', 'html')
('Data:', '\n\n')
None
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 urllib2 并添加这样的标头。至少你不会得到 403)) 在你的情况下
,
不要忘记导入库。祝你好运!
Try to use urllib2 and add headers like this. At least you won't get the 403)) And in your case
instead of
and don't forget import the library. Good luck!
考虑使用实际的 API。
试试这个:
它应该返回文章文本的 HTML 文档。
Consider using the actual API.
Try this:
It should return a HTML document of the text of the article.
确保您伪造了您的用户代理。维基百科不喜欢 Python 或 Perl,因此任何以“lwp”或“python-urllib”开头的用户代理都会遇到“临时”“技术问题”。
viltnan 提供的代码可以做到这一点。不过,他并没有真正具体说明为什么伪造用户代理,所以我想指出,访问大多数网站时通常没有必要,但访问维基百科时却有必要。
Make sure you forge your user agent. Wikipedia doesn't like Python or Perl, so any User-Agent that starts with "lwp" or "python-urllib" will get a "temporary" "technical problem."
The code provided by eviltnan does this. He didn't really specify why he forged the user agent, though, so I wanted to point out that it usually is not necessary when accessing most sites, but is necessary when accessing Wikipedia.