urllib.request.urlopen()socket.gaierror:[Errno -2]名称或服务未知

时间:2016-07-12 07:02:08

标签: python python-3.x urllib

我创建了非常简单的程序,可以访问url并下载html。 有2,000个网址。 (网站所有者接受访问权限。) 我遇到了以下错误。

socket.gaierror: [Errno -2] Name or service not known

错误发生的时间不同。 我无法理解错误导致的错误python3urlliblinux。 您有任何想法来检查错误吗?

错误日志

type:<class 'urllib.error.URLError'>
args:(gaierror(-2, 'Name or service not known'),)
Traceback (most recent call last):
File "/usr/local/lib/python3.5/urllib/request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/local/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/usr/local/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/usr/local/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/usr/local/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/usr/local/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/usr/local/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/local/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/local/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

import codecs
from urllib import request
import traceback

class Accessor():

    def __init__(self):
        pass

    def convert(self):

        beforeDataFile = codecs.open("url.csv","r", "cp932")
        cnt=1
        for line in beforeDataFile:
            try:
                cnt=cnt+1
                request_url = line
                print(request_url)
                response = request.urlopen(request_url)
                print(cnt,request_url,response)
            except Exception as e:
                print("=== ERROR ===")
                print("type:" + str(type(e)))
                print('args:' + str(e.args))
                print("message:" + e.message)
                print(traceback.format_exc())

if __name__ == "__main__":

     a = Accessor()
     a.convert()

url.py

   1 https://www.google.co.jp/
   2 https://www.google.co.jp/
   3 https://www.google.co.jp/
   4 https://www.google.co.jp/
   5 https://www.google.co.jp/
~~~
1995 https://www.google.co.jp/
1996 https://www.google.co.jp/
1997 https://www.google.co.jp/
1998 https://www.google.co.jp/
1999 https://www.google.co.jp/
2000 https://www.google.co.jp/

环境

CentOS 7.1.1503 Python3.5.1

0 个答案:

没有答案