Python urllib.request.urlopen()socket.gaError:[Errno-2]名称或服务未知
我创建了一个非常简单的程序,可以访问url并下载html。 有2000个URL。(网站所有者接受访问。) 我遇到了以下错误Python urllib.request.urlopen()socket.gaError:[Errno-2]名称或服务未知,python,python-3.x,urllib,Python,Python 3.x,Urllib,我创建了一个非常简单的程序,可以访问url并下载html。 有2000个URL。(网站所有者接受访问。) 我遇到了以下错误 socket.gaierror: [Errno -2] Name or service not known 错误不是在同一时间发生的。 我无法理解是哪个错误导致了python3,urllib,linux。 你有什么办法检查错误吗 错误日志 url.py 环境 CentOS 7.1.1503 Python3.5.1您应该输出失败的URL。不确定它如何与使用urllib2的U
socket.gaierror: [Errno -2] Name or service not known
错误不是在同一时间发生的。
我无法理解是哪个错误导致了python3
,urllib
,linux
。
你有什么办法检查错误吗
错误日志
url.py
环境
CentOS 7.1.1503
Python3.5.1您应该输出失败的URL。不确定它如何与使用urllib2的URL上的换行符一起工作。据我所知,这两种情况下都会因为相同的原因失败。DNS查找取决于网络和缓存可用性。这不是Python 3 vs 2的问题,这是DNS查找问题。什么主机名失败了?谢谢。我尝试使用虚拟url
https://www.google.co.jp/
,错误不仅发生在Python3.5.1上,而且发生在Python2.7.1上。我更改了问题标题。您应该输出失败的URL。不确定它如何与使用urllib2的URL上的换行符配合使用。据我所知,这两种情况下都会因为相同的原因失败。DNS查找取决于网络和缓存可用性。这不是Python 3 vs 2的问题,这是DNS查找问题。什么主机名失败了?谢谢。我尝试使用虚拟urlhttps://www.google.co.jp/
,错误不仅发生在Python3.5.1上,而且也发生在Python2.7.1上。我更改了问题标题。
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()
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/