urllib.error.URLError: <urlopen error no host given> python 3

Jasher picture Jasher · Jul 22, 2014 · Viewed 14.5k times · Source

I have this code in python 3.4

import urllib.request


def read_text():
    document = open(r"C:\mystuff\udacity\pruebatxt.txt")
    lec = document.read()
    document.close()
    print(lec)
    check_profanity(lec)

def check_profanity (text_to_check):
    print(text_to_check)

finally, i found the sollution by changing follow line

    lecture = urllib.request.urlopen("http://www.wdyl.com/profanity?q="+ urllib.parse.quote(text_to_check))
    output=lecture .read()
    print(output)

    lecture.close()
read_text()

And it gives me this error:

  File "C:\Python34\lib\urllib\request.py", line 1159, in do_request_
    raise URLError('no host given')
urllib.error.URLError: <urlopen error no host given>

What could be wrong?

Answer

Martijn Pieters picture Martijn Pieters · Jul 22, 2014

urlopen() returns a response, not a request. You cannot pass that to urlopen() again.

Remove the redundant urlopen() call:

lecture = urllib.request.urlopen("http://www.wdyl.com/profanity?q="+text_to_check)
output = lecture.read()

You may have a broken proxy configuration; verify that your proxies are sane by inspecting what Python finds in the Windows registry with:

print(urllib.request.getproxies())

or bypass proxy support altogether with:

lecture = urllib.request.urlopen(
    "http://www.wdyl.com/profanity?q="+text_to_check
    proxies={})