gpt4 book ai didi

api - 错误youtube v3 python响应未准备好

转载 作者:行者123 更新时间:2023-12-03 06:31:46 25 4
gpt4 key购买 nike

我使用python脚本通过youtube v3 api搜索视频信息。在一台计算机上,脚本可以完美运行,但在另一台计算机上,则收到以下错误:

File "script.py", line 105, in youtube_search(options) File "script.py", line 16, in youtube_search developerKey = DEVELOPER_KEY ..... File "C:\Python27\lib\httplib.py", line 1013, in getresponse raise ResponseNotReady() httplib.ResponseNotReady.



我正在使用的youtube_search()函数是:
def youtube_search(options):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)

search_response = youtube.search().list(
q = options.q,
part = "id, snippet",
maxResults=options.maxResults
).execute()

videos = []
channels = []
playlists = []
videoInfo = []

t = datetime.datetime.now()
ff_name = '[' + options.q + '].txt'
f = open(ff_name, 'w')

no_results = search_response.get("pageInfo")["totalResults"]
page = 1
while (page <= (no_results / 50)):
nextPage = search_response.get("nextPageToken")
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
info = youtube.videos().list(
part = "statistics,contentDetails"
,id = search_result["id"]["videoId"]).execute()
for info_result in info.get("items", []):
videos.append("%s<|>%s<|>%s" % (
time.strftime("%x")
,nextPage
,search_result["snippet"]["title"]
)
f.write(str(videos))
f.write('\n')
videos = []
page = page + 1
search_response = youtube.search().list(
q = options.q,
part = "id,snippet",
maxResults = options.maxResults,
pageToken = nextPage
).execute()

您对我为什么会遇到这种行为有任何暗示吗?

谢谢。

最佳答案

该特定异常在Python httplib ResponseNotReady中进行了解释

需要指出的是,您不需要为每个视频ID单独执行youtube.videos.list()调用。您最多可以将50个逗号分隔的视频ID作为id=参数传递给单个youtube.videos.list()调用。减少正在发出的HTTP请求的数量将导致更好的性能,并且可以解决该异常。

关于api - 错误youtube v3 python响应未准备好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19775764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com