3

我目前正在尝试向 YouTube 发出 GET 请求,以在其上搜索视频(使用 Python 以及Requests模块)。以下是我正在做的请求:

r = requests.get("http://www.youtube.com/", 
        params={
            "search_query": "Test"
        }).text

但是,在打印请求时,无论我将搜索查询更改为什么,它似乎都只是获取 YouTube 的主页。

有人知道为什么会这样吗?

谢谢

4

3 回答 3

4

看 youtube,搜索页面的 url 似乎是

http://www.youtube.com/results?search_query=test

您缺少该results部分,即您要查找的页面。

于 2013-07-07T22:14:04.477 回答
3

使用 Youtube API 获取数据。

# Import the modules
import requests
import json

# Make it a bit prettier..
print "-" * 30
print "This will show the Most Popular Videos on YouTube"
print "-" * 30

# Get the feed
r = requests.get("http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?v=2&alt=jsonc")
r.text

# Convert it to a Python dictionary
data = json.loads(r.text)

# Loop through the result. 
for item in data['data']['items']:

    print "Video Title: %s" % (item['title'])

    print "Video Category: %s" % (item['category'])

    print "Video ID: %s" % (item['id'])

    print "Video Rating: %f" % (item['rating'])

    print "Embed URL: %s" % (item['player']['default'])

    print

通过http://www.pythonforbeginners.com/python-on-the-web/using-the-youtube-api/了解更多详情。

于 2013-07-07T22:15:46.893 回答
1

试试这个看看你实际提交的是什么 print r.url

于 2013-07-07T22:32:04.777 回答