Cisplatin
Cisplatin

Reputation: 2998

How to send parameters with a GET request?

I'm currently trying to make a GET request to YouTube, to search for a video on it (using Python, as well as the Requests module). The following is what I'm doing to make a request:

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

However, when printing the request, it only seems to be getting the homepage of YouTube, no matter what I change the search query to.

Would anyone know why this is occuring?

Thanks

Upvotes: 3

Views: 31685

Answers (3)

Alan Chapman
Alan Chapman

Reputation: 11

Try this to see what you are actually submitting print r.url

Upvotes: 1

Zero
Zero

Reputation: 76917

Use the Youtube API to get the data.

# 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

Go through http://www.pythonforbeginners.com/python-on-the-web/using-the-youtube-api/ for more details.

Upvotes: 3

apartridge
apartridge

Reputation: 1820

Looking at youtube, the url to the search page seems to be

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

You're missing the results part, which is the page you are looking for.

Upvotes: 6

Related Questions