【发布时间】:2015-04-21 00:59:59
【问题描述】:
我在 python 中使用 html2text 通过获取任何 URL 来获取 HTML 页面的原始文本(包括标签),但出现错误。
我的代码 -
import html2text
import urllib2
proxy = urllib2.ProxyHandler({'http': 'http://<proxy>:<pass>@<ip>:<port>'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
html = urllib2.urlopen("http://www.ndtv.com/india-news/this-stunt-for-a-facebook-like-got-the-hyderabad-youth-arrested-740851").read()
print html2text.html2text(html)
错误——
Traceback (most recent call last):
File "t.py", line 8, in <module>
html = urllib2.urlopen("http://www.ndtv.com/india-news/this-stunt-for-a-facebook-like-got-the-hyderabad-youth-arrested-740851").read()
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 110] Connection timed out>
谁能解释我做错了什么?
【问题讨论】:
-
这和html2text没有任何关系;这是 URL 提取中的错误。您可以通过浏览器加载该 URL 吗?你能再试一次吗?像这样的网络错误通常是间歇性的。
-
是的,它在浏览器上运行良好......任何其他建议......??
-
urllib2.urlopen 已经为您提供了文本;这个错误我不知道。
-
这个错误意味着你的脚本等待了很长时间但是服务器没有说什么。
-
您需要改进拼写和大小写。我被禁止了一次。