<python编程从入门到实践》第十六章 下载数据 16.2.1 运行报错
from __future__ import (absolute_import, division, print_function,
unicode_literals)
try:
# Python 2.x 版本
from urllib2 import urlopen
except ImportError:
# Python 3.x 版本
from urllib.request import urlopen # 1
import json
import requests
import pygal
import math
from itertools import groupby
json_url = 'https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
response = urlopen(json_url) # 2
# 读取数据
req = response.read()
# 将数据写入文件
with open('btc_close_2017_urllib.json', 'wb') as f: # 3
f.write(req)
# 加载json格式
file_urllib = json.loads(req.decode('utf8')) # 4
print(file_urllib)
json_url = 'https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
req = requests.get(json_url) # 1
# 将数据写入文件
with open('btc_close_2017_request.json', 'w') as f:
f.write(req.text) # 2
file_requests = req.json() # 3
print(file_urllib == file_requests)
Traceback (most recent call last):
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 1348, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1328, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1037, in _send_output
self.send(msg)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 975, in send
self.connect()
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1447, in connect
super().connect()
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 941, in connect
self.sock = self._create_connection(
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\Desktop\书-python编程从入门到实践随书下载\《Python编程》源代码文件\chapter_16\btc-master (16.2)\btc_close_2017.py", line 23, in <module>
response = urlopen(json_url) # 2
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 519, in open
response = self._open(req, data)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 536, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 1391, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "C:\Users\Administrator.DESKTOP-5CJ81BQ\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 1351, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
查过方法,说是DNS问题,修改以后也没有用,然后挂了代理,又出现了ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:997。不知道怎么解决了
我想要达到的结果