Jasher Jasher - 6 months ago 140
Python Question

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

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

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={})