2013-07-07 104 views
2

我正在嘗試向YouTube發出GET請求,以搜索其上的視頻(使用Python以及請求模塊)。以下是我在做什麼,使一個請求:如何使用GET請求發送參數?

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

然而,打印請求時,它只是似乎越來越YouTube首頁,無論什麼我更改搜索查詢。

有人會知道爲什麼會發生這種情況嗎?

感謝

回答

3

縱觀YouTube的鏈接到搜索的頁面可能

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

你錯過了results的一部分,這是你正在尋找的頁面。

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/瞭解更多詳情。

1

試試看看你實際提交的是什麼 print r.url