如何使用youtubeapi通过viewCount获得“正确”的视频顺序

2024-03-29 13:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Youtube数据API按浏览量的顺序来获取视频,但得到了错误的列表

我用Python调用了Youtube数据API的search(),得到了列表

代码如下:

def youtube_search(options):
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)

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

  videos = []

  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videos.append(search_result["id"]["videoId"])

  for video in range(len(videos)):
    videos_response = youtube.videos().list(
      part="id,snippet,statistics",
      id=videos[video]
    ).execute()

    for count in videos_response.get("items", []):
      print("{} {}".format(count["snippet"]["title"], count["statistics"]["viewCount"]))

if __name__ == "__main__":
  argparser.add_argument("--max-results", help="Max results", default=10)
  argparser.add_argument("--order", help="Order", default="viewCount")
  argparser.add_argument("--type", help="Type", default="video")
  args = argparser.parse_args()

  try:
    youtube_search(args)
  except HttpError as e:
    print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))

结果如下:(title viewcount)

Queen – Bohemian Rhapsody (Official Video Remastered) 1044540893
Crushing Crunchy & Soft Things by Car! - Floral Foam, Squishy, Tide Pods and More! 979803252
Chris Brown - Loyal (Explicit) ft. Lil Wayne, Tyga 923661647
Anuel AA, KAROL G - Secreto 864893341
Backstreet Boys - I Want It That Way 690126303
AC/DC - Thunderstruck (Official Video) 651299656
Ping Pong Trick Shots 3 | Dude Perfect 232359979
DJ (Duvvada Jagannadham) Full Hindi Dubbed Movie | Allu Arjun, Pooja Hegde 182039396
Tape Face Auditions & Performances | America's Got Talent 2016 Finalist 157947450
lofi hip hop radio - beats to relax/study to 154225661

然而,根据维基百科,似乎有更多的看法视频。 (https://en.wikipedia.org/wiki/List_of_most-viewed_YouTube_videos

我试图定义“regionCode”,例如“ES”,但是结果没有改变

请帮我解决这个问题


Tags: apiidforsearchyoutuberesponsevideotype