我一直在尝试用python创建YouTube排名检查器。想法是,它在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)
最佳答案
想通了,这是万一有人想要的工作代码:
http://pastebin.com/Vei3FyUz
如果您已登录,YouTube也会个性化结果,因此请确保在检查准确性时注销。
关于python - 在Python中使用YouTube API按相关性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17893917/