[Tutor] Thanks for the advise to tutor! HTTP Error 400 resolved
cm
cm12789 at gmail.com
Tue Feb 13 19:31:04 EST 2018
I have added below to the first line of the function and managed to
ignore the space and solve it. Thank you for the hint!
some_text=urllib.parse.quote_plus(some_text)
On Tue, Feb 13, 2018 at 5:00 PM, <tutor-request at python.org> wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
> 1. Re: [Help] urllib.error.HTTPError: HTTP Error 400: Bad
> Request (Peter Otten)
>
>
> ---------- Forwarded message ----------
> From: Peter Otten <__peter__ at web.de>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Tue, 13 Feb 2018 14:47:56 +0100
> Subject: Re: [Tutor] [Help] urllib.error.HTTPError: HTTP Error 400: Bad Request
> 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 at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list