cm wrote: > Dear tutors, > > I have written below function to open the profanity check url and then > to check for profanity in some text. When I go to the url > http://www.wdylike.appspot.com/?q= and type in the same text, it works > fine. > > I am using Microsoft OS X and Python 3.5.2 Interpreter with Pycharm > Community Edition. > > --- > import urllib.request > > def check_profanity(some_text): > # check text for a curse word > connection = > urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+some_text) > output = connection.read() > print(output) > connection.close() > > check_profanity(some_text="I gave it a good shot") > --- > > Error message: > Traceback (most recent call last): > File > "C:/Users/Administrator/PycharmProjects/Udacity/profanity_check.py", > line 29, in <module> > check_profanity(some_text="I gave it a good shot") > File > "C:/Users/Administrator/PycharmProjects/Udacity/profanity_check.py", > line 15, in check_profanity > connection = > urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+some_text) > File "C:\Program Files\Anaconda3\lib\urllib\request.py", line 163, in > urlopen > return opener.open(url, data, timeout) > File "C:\Program Files\Anaconda3\lib\urllib\request.py", line 472, in > open > response = meth(req, response) > File "C:\Program Files\Anaconda3\lib\urllib\request.py", line 582, > in http_response > 'http', request, response, code, msg, hdrs) > File "C:\Program Files\Anaconda3\lib\urllib\request.py", line 510, in > error > return self._call_chain(*args) > File "C:\Program Files\Anaconda3\lib\urllib\request.py", line 444, > in _call_chain > result = func(*args) > File "C:\Program Files\Anaconda3\lib\urllib\request.py", line 590, > in http_error_default > raise HTTPError(req.full_url, code, msg, hdrs, fp) > urllib.error.HTTPError: HTTP Error 400: Bad Request > > Process finished with exit code 1 > > --- > However when I run the code it just says Bad Request. I tried to read > into the traceback message but it refers not only to my file but the > urllib function itself too and I can't understand.
Spaces aren't allowed in the url: >>> c = rq.urlopen("http://www.wdylike.appspot.com/?q=nice try") [...] urllib.error.HTTPError: HTTP Error 400: Bad Request Once you escape the ' ': >>> c = rq.urlopen("http://www.wdylike.appspot.com/?q=nice+try") >>> c.read() b'false' Have a look at the first example at https://docs.python.org/dev/library/urllib.request.html#urllib-examples for a more general solution. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor