ken*_*r99 5 python exception-handling beautifulsoup utf8-decode web-scraping
我正在编写一个脚本,该脚本转到链接列表并解析信息。
它适用于大多数站点,但它在某些站点上令人窒息,“UnicodeEncodeError:'ascii' 编解码器无法在位置 13 中编码字符 '\xe9':序号不在范围内(128)”
它在 client.py 上停止,它是 python3 上 urlib 的一部分
确切链接是:http : //finance.yahoo.com/news/cafés-growth-faster-than-fast-food-peers-144512056.html
这里有很多类似的帖子,但似乎没有一个答案对我有用。
我的代码是:
from urllib import request
def __request(link,debug=0):
try:
html = request.urlopen(link, timeout=35).read() #made this long as I was getting lots of timeouts
unicode_html = html.decode('utf-8','ignore')
# NOTE the except HTTPError must come first, otherwise except URLError will also catch an HTTPError.
except HTTPError as e:
if debug:
print('The server couldn\'t fulfill the request for ' + link)
print('Error code: ', e.code)
return ''
except URLError as e:
if isinstance(e.reason, socket.timeout):
print('timeout')
return ''
else:
return unicode_html
Run Code Online (Sandbox Code Playgroud)
链接 = ' http://finance.yahoo.com/news /cafés-gromming-faster-than-fast-food-peers-144512056.html' 页面 = __request(link)
回溯是:
Traceback (most recent call last):
File "<string>", line 250, in run_nodebug
File "C:\reader\get_news.py", line 276, in <module>
main()
File "C:\reader\get_news.py", line 255, in main
body = get_article_body(item['link'],debug=0)
File "C:\reader\get_news.py", line 155, in get_article_body
page = __request('na',url)
File "C:\reader\get_news.py", line 50, in __request
html = request.urlopen(link, timeout=35).read()
File "C:\Python33\Lib\urllib\request.py", line 156, in urlopen
return opener.open(url, data, timeout)
File "C:\Python33\Lib\urllib\request.py", line 469, in open
response = self._open(req, data)
File "C:\Python33\Lib\urllib\request.py", line 487, in _open
'_open', req)
File "C:\Python33\Lib\urllib\request.py", line 447, in _call_chain
result = func(*args)
File "C:\Python33\Lib\urllib\request.py", line 1268, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python33\Lib\urllib\request.py", line 1248, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python33\Lib\http\client.py", line 1061, in request
self._send_request(method, url, body, headers)
File "C:\Python33\Lib\http\client.py", line 1089, in _send_request
self.putrequest(method, url, **skips)
File "C:\Python33\Lib\http\client.py", line 953, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 13: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏这让我发疯,我想我已经尝试了 x.decode 和类似的所有组合
(如果可能的话,我可以忽略有问题的字符。)
使用百分比编码的 URL:
\n\nlink = \'http://finance.yahoo.com/news/caf%C3%A9s-growing-faster-than-fast-food-peers-144512056.html\'\nRun Code Online (Sandbox Code Playgroud)\n\n我通过将浏览器指向上面的百分比编码 URL
\n\nhttp://finance.yahoo.com/news/caf\xc3\xa9s-growing-faster-than-fast-food-peers-144512056.html\nRun Code Online (Sandbox Code Playgroud)\n\n转到该页面,然后将浏览器提供的编码 URL 复制并粘贴回文本编辑器中。但是,您可以使用以下命令以编程方式生成百分比编码的 URL:
\n\nfrom urllib import parse\n\nlink = \'http://finance.yahoo.com/news/caf\xc3\xa9s-growing-faster-than-fast-food-peers-144512056.html\'\n\nscheme, netloc, path, query, fragment = parse.urlsplit(link)\npath = parse.quote(path)\nlink = parse.urlunsplit((scheme, netloc, path, query, fragment))\nRun Code Online (Sandbox Code Playgroud)\n\n这产生
\n\nhttp://finance.yahoo.com/news/caf%C3%A9s-growing-faster-than-fast-food-peers-144512056.html\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3426 次 |
| 最近记录: |