2

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?

Jasher
  • 31
  • 1
  • 1
  • 4

2 Answers2

3

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={})
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

Your code returns a binary string. It would be better to decode it into ascii format:

with urllib.request.urlopen("http://www.wdyl.com/profanity?q=" + urllib.parse.quote(text_to_check)) as response:
    output = response.read()
output = output.decode('ascii')
print (output)
Fahad Haleem
  • 689
  • 6
  • 5