ウェブサイトからソースコードをインポートしようとしましたが、インポートしようとしている文字の1つである問題がî
で、エラーの原因となっています。ここでHTMLコードをインポートするユニコードエラー
は私のコードです:ここでは
import urllib.request
htmlfile = urllib.request.urlopen("url...")
htmltext=htmlfile.read()
print(htmltext)
は誤りです:
Traceback (most recent call last):
File "/Users/****/Documents/Scraping.py", line 3, in <module>
htmlfile = urllib.request.urlopen("http://*****")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1250, in _send_request
self.putrequest(method, url, **skips)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1117, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xee' in position 32: ordinal not in range(128)
はまた、この問題が発生した:
"http://...url...".encode('ascii')
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
"http://www....url...".encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xee' in position 50: ordinal not in range(128)
完全なスタックトレースを投稿できますか?どのラインにエラーがありましたか? – tdelaney
投稿内に更新されました –
これはGETリクエストの送信に問題があるようです。 URL自体に何か奇妙なことはありますか? url "" url ... encode( 'ascii') 'を手動でエンコードしてみてください。 – tdelaney