0

I have following code in python 3.5.

import urllib.request

prof_list = open(r"C:\local\profanity.txt")
contents = prof_list.read()
connect = urllib.request.urlopen("http://wdyl.com/profanity?q="+contents)
output = connect.read()
connect.close()
prof_list.close()

when I run the code, I get

 raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

Could not figure despite multiple efforts.

1
  • what is the content of profanity.txt ? Commented Nov 10, 2015 at 2:30

2 Answers 2

1

You need to pass one extra argument to urllib.request.urlopen like shown in the example below:

urllib.request.urlopen('someurl.com', data=None)  # if you have extra data to send to the server

Please take a look at this PAGE to know more about the different information to send to a server.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to encode the query string before using it in the url.

from urllib.parse import quote
encoded_query=quote(query)

Then you can use

urllib.request.urlopen("http://wdyl.com/profanity?q="+encoded_query)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.