【发布时间】:2022-10-08 17:00:36
【问题描述】:
我对此很陌生,我花了几天的时间才走到这一步,我现在拥有的将 JSON 提要推送到 Google 表格中的脚本适用于我的测试链接,但在与我实际需要提取的 URL 一起使用时会超时从。
我可以确认真正的 URL 有效,并且我可以访问 - 我可以毫无问题地打印到终端。
它有敏感信息,所以我无法分享——我已经研究了代理和 URI,但还没有真正能够用我的代码弄清楚其中的任何一个。
# import urllib library
import json
from urllib.request import urlopen, Request
import gspread
import requests
gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('1-1aiGMn2yUWRlh_jnIebcMNs-6phzUNxkktAFH7uY9o')
worksheet = sh.sheet1
url = 'URL LINK GOES HERE'
# store the response of URL
response = urlopen(Request(url, headers={"User-Agent": ""}))
r = requests.get("URL LINK GOES HERE",
proxies={"http": "http://61.233.25.166:80"})
# storing the JSON response
# from url in data
data_json = json.loads(response.read())
# print the json response
# print(data_json)
result = []
for key in data_json:
result.append([key, data_json[key] if not isinstance(
data_json[key], list) else ",".join(map(str, data_json[key]))])
worksheet.update('a1', result)
# proxies///uris///url 100% works
有人对我如何避免超时有建议吗?完整错误如下:
Traceback (most recent call last):
File "c:\Users\AMadle\NBA-JSON-Fetch\2PrintToSheetTimeoutTesting.py", line 17, in <module>
response = urlopen(Request(url, headers={"User-Agent": ""}))
File "C:\Python\python3.10.5\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "C:\Python\python3.10.5\lib\urllib\request.py", line 519, in open
response = self._open(req, data)
File "C:\Python\python3.10.5\lib\urllib\request.py", line 536, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\Python\python3.10.5\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "C:\Python\python3.10.5\lib\urllib\request.py", line 1391, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "C:\Python\python3.10.5\lib\urllib\request.py", line 1351, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
【问题讨论】:
-
你在终端打印什么?您究竟是如何确认您的网址有效的?
-
我正在打印一个我无法共享的工作链接,但我通过将相同的 API 打印到终端来确认它可以工作。当我尝试推送到表格 @koolkoda 时,它会超时
标签: python json request timeout gspread