kaggle 内核:urllib.request.urlopen 不适用于任何 url [英] kaggle kernels: urllib.request.urlopen not working for any url
问题描述
在 kaggle kernels
中处理获取 url 列表的最佳方法是什么?
What is the best way to handle fetching of list of urls in kaggle kernels
?
我首先尝试使用 google.com
进行测试.
I tried first testing with google.com
.
第一种方法
:使用 urllib.request
First Method
: Using urllib.request
import urllib.request resp = urllib.request.urlopen('http://www.google.com')
这会导致 gai 和 urlopen 错误 [Errno -2] Name or service not known
This lead to error of gai and urlopen error [Errno -2] Name or service not known
第二种方法
:使用请求
import requests resp = requests.get('http://www.google.com')
这会导致错误 gaierror: [Errno -3] 名称解析临时失败和无法建立新连接:[Errno -3] 名称解析临时失败.
This lead to error gaierror: [Errno -3] Temporary failure in name resolution and Failed to establish a new connection: [Errno -3] Temporary failure in name resolution.
import urllib.request
req = urllib.request.Request('http://www.google.com')
print (req)
try:
response = urllib.request.urlopen(req)
print (response)
except urllib.error.URLError as e:
print (e.reason)
print("something wrong")
输出:
<urllib.request.Request object at 0x7fed1d00c518>
[Errno -2] Name or service not known
something wrong
我尝试按照 stackoverflow 答案.
解决此错误的方法是什么?为什么 urlopen 或 requests 在 kaggle 内核中不起作用?
我见过许多内核都出现相同的错误 kernel 1 内核 2 内核 3.
What is the way around to fix this error? Why is urlopen or requests not working in kaggle kernels?
I have seen many kernels with the same errors kernel 1 kernel 2 kernel 3.
推荐答案
这对您不起作用的原因是因为 Kaggle Kernels 当前无法访问 Internet.因此,您无法在内核中进行需要网络连接的 API 调用.
The reason this isn't working for you is because Kaggle Kernels currently don't currently have internet access. As a result, there's not a way for you to make API calls that require a network connection from within kernels.
编辑 2018 年 8 月:仅供参考,我们现在添加了对内核的互联网访问.:) 您可以在编辑器的左侧栏中启用它.
Edit August 2018: Just FYI, we have now added internet access to Kernels. :) You can enable it in the left-hand side bar from within the editor.
这篇关于kaggle 内核:urllib.request.urlopen 不适用于任何 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!