王旭. 2022-07-08 22:46 采纳率: 66.7%
浏览 51
已结题

python编程从入门到实践. 下载数据出错

<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。不知道怎么解决了
我想要达到的结果
  • 写回答

5条回答 默认 最新

  • 天际的海浪 2022-07-08 22:55
    关注
    获得1.40元问题酬金

    你这个代码老了
    应该是人家网站改版过了, 这个要爬取数据的网站地址或参数已经变更了,

    https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json 地址现在已经失效打不开了

    你这个代码已经过期
    要爬取什么数据, 需要从现在的网站重新在有这个数据的页面中重新找到改版过后的数据地址和参数,
    如果数据json字典的格式也变了,还需要重新分析json字典写对应的代码从json字典中提取数据

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月16日
  • 创建了问题 7月8日

悬赏问题

  • ¥15 MPLAB IDE V2.35 报错make[2]: *** [build/default/production/_ext/1472/MSSP_I2C.p1] Error 1
  • ¥15 新唐M451 DTMF检测和发送代码修改
  • ¥15 在国外文献网站里点击view pdf 加载异常缓慢甚至加载不出来。
  • ¥65 python批量提取发票的信息
  • ¥15 虚幻五引擎内容如何上传至网盘?
  • ¥15 使用mmpose库时出现了问题
  • ¥15 IRI2016模型matlab运行报错
  • ¥50 bat怎么设置电脑后台自动点击网页指定词运行脚本,输入指定网页链接,指定点击词,指定间隔时间,指定网页出现的词,指定网页出现词出现后后点击锁定,放在后台运行不影响前台鼠标工作
  • ¥20 20CrMnMo的高温变形抗力
  • ¥15 RTX3.6 5565驱动中断报错