Fabricio Lopes
Fabricio Lopes

Reputation: 19

How to solve this problem with request.get in python?

import urllib.request
import urllib.parse
import re

import requests
def pesquisar(string):
    str = string.replace(" ", "+")
    print(str)
    searching = urllib.request.urlopen("https://www.youtube.com/results?search_query={}".format(str))
    search_result = requests.get(searching).text
    print(search_result)

I'm building a function which receives a phrase and searches for youtube videos which contain this words, but I'm getting this error:

requests.exceptions.MissingSchema: Invalid URL '<http.client.HTTPResponse object at 0x000002452B723100>': No schema supplied. Perhaps you meant http://<http.client.HTTPResponse object at 0x000002452B723100>?

Any hints of how to correct this?

Upvotes: 0

Views: 735

Answers (1)

Niel Godfrey P. Ponciano
Niel Godfrey P. Ponciano

Reputation: 10699

requests.get accepts a URL. But you are passing it the response of urllib.request.urlopen which is not a URL but a http.client.HTTPResponse. See the possible methods for http.client.HTTPResponse here.

Actually, since you already used urllib.request.urlopen to get the URL, no need to call requests.get. Choose only one. So either through requests which is preferrable:

...
searching = requests.get("https://www.youtube.com/results?search_query={}".format(str))
search_result = searching.text
print(search_result)
...

Or if you really want to use urllib:

...
searching = urllib.request.urlopen("https://www.youtube.com/results?search_query={}".format(str))
search_result = searching.read()
print(search_result)
...

Upvotes: 2

Related Questions