如何使用Google API每天从youtube抓取1万条记录

时间:2019-04-15 05:17:53

标签: python web-scraping google-api

我正在使用Google API使用youtube v3从youtube抓取数据。基于搜索关键字,我正在尝试抓取Likescount,viewscount,dislikescount等数据。

问题是默认情况下,我们最多可以获取50条记录。我需要获取更多记录,我们可以使用分页来实现。

在2019年1月11日,Google的每天记录从100万减少到了1万。要每天请求1万条记录,我们需要进行分页,但我不确定如何在代码中设置分页。

from apiclient.discovery import build
import argparse
import csv
import unidecode

DEVELOPER_KEY = "xxxxxxx"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

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.max_results).execute()

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

    csvFile = open('checking_for_no_of_records.csv','w')
    csvWriter = csv.writer(csvFile)
    csvWriter.writerow(["title","videoId","viewCount","likeCount","dislikeCount", "commentCount","favoriteCount"])

    for search_result in search_response.get("items", []):
        if search_result["id"]["kind"] == "youtube#video":
            title = search_result["snippet"]["title"]
            title = unidecode.unidecode(title)
            videoId = search_result["id"]["videoId"]
            video_response = youtube.videos().list(id=videoId,part="statistics").execute()
            for video_result in video_response.get("items",[]):
                    viewCount = video_result["statistics"]["viewCount"]
                    if 'likeCount' not in video_result["statistics"]:
                            likeCount = 0
                    else:
                            likeCount = video_result["statistics"]["likeCount"]
                    if 'dislikeCount' not in video_result["statistics"]:
                            dislikeCount = 0
                    else:
                            dislikeCount = video_result["statistics"]["dislikeCount"]
                    if 'commentCount' not in video_result["statistics"]:
                            commentCount = 0
                    else:
                            commentCount = video_result["statistics"]["commentCount"]
                    if 'favoriteCount' not in video_result["statistics"]:
                            favoriteCount = 0
                    else:
                            favoriteCount = video_result["statistics"]["favoriteCount"]
            csvWriter.writerow([title,videoId,viewCount,likeCount,dislikeCount, commentCount,favoriteCount])

    csvFile.close()  


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--q', help='Search term', default='Google')
  parser.add_argument('--max-results', help='Max results',default = 50)
  args = parser.parse_args()

  youtube_search(args)

使用上面的代码,我只能获得50条记录,每天需要获得1万条记录。

0 个答案:

没有答案