我一直在尝试在 python 中创建一个 YouTube Rank Checker。这个想法是它在 YouTube 上搜索关键字并检查视频的位置。
唯一的问题是它没有返回准确的结果。例如,如果我搜索“狗视频”并且当我使用普通网站搜索时我的视频是位置 3,并且 API 将其返回为位置 17。我想这可能是因为它对结果的排序方式不同(它应该按相关性),但我不知道如何确定排序顺序。
如果有人可以提供任何帮助,即使只是在我的代码中指出一些明显的东西,我也会非常感激,提前感谢您的帮助。
这是我的代码:
from apiclient.discovery import build
from optparse import OptionParser
import time
# Set DEVELOPER_KEY to the "API key" value from the "Access" tab of the
# Google APIs Console http://code.google.com/apis/console#access
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = "devkey"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
videos = []
channels = []
playlists = []
videoids = []
videoidskeys = {"cat video":"ZBAGEeOms-8", "dog video":"nGeKSiCQkPw", "testing video":"GAyni_fKKLg"}
videokeys = videoidskeys.keys()
def checkrank(videoid, keyword):
time.sleep(6)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("--q", dest="q", help="Search term",
default=keyword)
parser.add_option("--max-results", dest="maxResults",
help="Max results", default=25)
(options, args) = parser.parse_args()
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()
for search_result in search_response.get("items", []):
videoids.append(search_result["id"]["videoId"])
count = 0
for videoid1 in videoids:
count = count+1
if videoid1 == videoid:
return "Keyword: %s - Rank: %s" % (keyword, count)
count = 0
for video in videokeys:
print checkrank(videoidskeys[video],video)