よろしくお願いします。
python3.5使ってホームページのソースコードを表示させるプログラムを組もうと思っています。
ネットで調べて適当なサンプルを動かしてみたのですがエラーが出て動きません。
Ptyhon
1import urllib.request 2 3import urllib.request 4response = urllib.request.urlopen("https://www.yahoo.co.jp/") 5result = response.read() 6print(result)
Python
1 2>>> %Run getHTML.py 3Traceback (most recent call last): 4 File "C:\Thonny\lib\urllib\request.py", line 1318, in do_open 5 encode_chunked=req.has_header('Transfer-encoding')) 6 File "C:\Thonny\lib\http\client.py", line 1239, in request 7 self._send_request(method, url, body, headers, encode_chunked) 8 File "C:\Thonny\lib\http\client.py", line 1285, in _send_request 9 self.endheaders(body, encode_chunked=encode_chunked) 10 File "C:\Thonny\lib\http\client.py", line 1234, in endheaders 11 self._send_output(message_body, encode_chunked=encode_chunked) 12 File "C:\Thonny\lib\http\client.py", line 1026, in _send_output 13 self.send(msg) 14 File "C:\Thonny\lib\http\client.py", line 964, in send 15 self.connect() 16 File "C:\Thonny\lib\http\client.py", line 1392, in connect 17 super().connect() 18 File "C:\Thonny\lib\http\client.py", line 936, in connect 19 (self.host,self.port), self.timeout, self.source_address) 20 File "C:\Thonny\lib\socket.py", line 704, in create_connection 21 for res in getaddrinfo(host, port, 0, SOCK_STREAM): 22 File "C:\Thonny\lib\socket.py", line 743, in getaddrinfo 23 for res in _socket.getaddrinfo(host, port, family, type, proto, flags): 24socket.gaierror: [Errno 11004] getaddrinfo failed 25 26During handling of the above exception, another exception occurred: 27 28Traceback (most recent call last): 29 File "C:\Users\xxxxx\Desktop\getHTML.py", line 4, in <module> 30 response = urllib.request.urlopen("https://www.yahoo.co.jp/") 31 File "C:\Thonny\lib\urllib\request.py", line 223, in urlopen 32 return opener.open(url, data, timeout) 33 File "C:\Thonny\lib\urllib\request.py", line 526, in open 34 response = self._open(req, data) 35 File "C:\Thonny\lib\urllib\request.py", line 544, in _open 36 '_open', req) 37 File "C:\Thonny\lib\urllib\request.py", line 504, in _call_chain 38 result = func(*args) 39 File "C:\Thonny\lib\urllib\request.py", line 1361, in https_open 40 context=self._context, check_hostname=self._check_hostname) 41 File "C:\Thonny\lib\urllib\request.py", line 1320, in do_open 42 raise URLError(err) 43urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed> 44>>>
どうも社内のProxyが邪魔しているのではないかとおもっているのですが、
PROXYの設定方法がよくわからないので教えてください。
解決
社内プロキシのせいで通信が出来なかった事が原因でした。
HTTP_PROXYにプロキシアドレスを設定してみましたが上手くいかなかった。
なので以下の方法でProxyを設定してみた所、取りあえず取得する事が出来た。
python
1import urllib.request 2 3PROXIES = { 4 'http' : 'http://proxy.example.co.jp:80080' # プロキシのアドレスを指定 5} 6proxy_handler = urllib.request.ProxyHandler(PROXIES) 7opener = urllib.request.build_opener(proxy_handler) 8data = opener.open('http://www.google.co.jp').read() 9print(data)
コメントで教えて頂いたRequestsパッケージの導入方法については別件で質問しました(解決済)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/16 09:38
2017/09/19 01:10
2017/09/22 09:56