python - 从 Sky Server 数据库下载图像时出错?
问题描述
我正在收集来自 Sloan Digital Sky Survey 的图像,并尝试将这些图像本地下载到一个文件夹中。这是我到目前为止的代码:
import urllib.request
import pandas as pd
def get_images(df_converted):
objid_list = df_converted['OBJID'].tolist()
ra_list = df_converted['RA_DECIMAL'].tolist()
dec_list = df_converted['DEC_DECIMAL'].tolist()
for objid, ra, dec in zip(objid_list, ra_list, dec_list):
# Save the image using the 'OBJID' as .jpg image
filename = str(objid) + '.jpg'
# Replace the ra and dec coordinates in the URL, downloading in 512 x 512 resolution
image_url = "http://skyservice.pha.jhu.edu/DR7/ImgCutout/getjpeg.aspx?ra=" + str(ra) + "&dec=" + str(dec) + "&scale=0.40&width=512&height=512&opt="
urllib.request.urlretrieve(image_url, "images/" + filename)
这段代码运行良好,我可以下载大约 600 张图像,但这些并不是我需要的全部图像。在某些时候,我遇到了以下错误:
shilpakancharla@Shilpas-MBP galaxy-classification % python3 sdss_image_capture.py
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
self.send(msg)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
self.connect()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/http/client.py", line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sdss_image_capture.py", line 87, in <module>
get_images(new_df)
File "sdss_image_capture.py", line 82, in get_images
urllib.request.urlretrieve(image_url, "images/" + filename)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open
'_open', req)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 60] Operation timed out>
我在理解Transfer-encoding
错误的一部分以及为什么会发生这种情况时遇到了一些麻烦,以及如何以能够处理此问题的方式编写我的方法?
解决方案
我通过以下方式获得了解决方案:
pip install ssl
然后将以下行添加到我的代码中:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
在我的驱动程序代码中。
推荐阅读
- c++ - 从 VSStudio 得到 C2628 奇怪的错误,所有分号都在那里
- apache-flink - 如何使状态巨大的 Flink 工作完成
- node.js - 如何在持续集成中管理 .env 配置
- python - 在 Pandas 中创建 Excel SUMIFS
- c++ - 在 C++ 中使用地图时“无法从...进行转换”
- python - Python not all arguments converted during string formatting?
- mysql - 无法通过计算相关表中的字段来更新表
- node.js - 如何在 NodeJS 中使用 Splunk-Logging
- angular - 用户以角度登录后显示侧边栏和仪表板组件
- docker - 拒绝:将图像推送到 gitlab 注册表时请求的资源访问被拒绝