Python Urllib2.httperror Http Error 400 Bad Request
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of urllib2.httperror http error 400 bad request json this site About Us Learn more about Stack Overflow the company Business Learn python 3 urllib.error.httperror http error 400 bad request more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Http Error 400: Bad Request Python Urllib
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up urllib2
Raise Httperror(req.get_full_url(), Code, Msg, Hdrs, Fp)
HTTP Error 400: Bad Request up vote 12 down vote favorite 5 I have a piece of code like this host = 'http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s' % (query, page) req = urllib2.Request(host) req.add_header('User-Agent', User_Agent) response = urllib2.urlopen(req) and when I input a query greater than one word like "the dog" i get the following error. response = urllib2.urlopen(req) File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, response 400 python requests data, timeout) File "/usr/lib/python2.7/urllib2.py", line 400, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 513, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 438, in error return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 400: Bad Request Can anyone point out what im doing wrong? Thanks in advance. python urllib2 http-error share|improve this question asked Jan 12 '12 at 18:27 PyFan 66115 I've also received "urllib2.HTTPError: HTTP Error 406: Not Acceptable" when attempting to request URLs with whitespace. –jamesc Oct 5 '12 at 9:27 add a comment| 4 Answers 4 active oldest votes up vote 48 down vote accepted The reason that "the dog" returns a 400 Error is because you aren't escaping the string for a URL. If you do this: import urllib, urllib2 quoted_query = urllib.quote(query) host = 'http://www.bing.com/search?q=%s&go=&qs=n&sk=&sc=8-13&first=%s' % (quoted_query, page) req = urllib2.Request(host) req.add_header('User-Agent', User_Agent) response = urllib2.urlopen(req) It will work. However I highly suggest you use requests instead of using urllib/urllib2/httplib. It's much much easier and it'll handle all of
here for a quick overview of the site Help Center Detailed answers to any
Fancyurlopener
questions you might have Meta Discuss the workings and policies of raise httperror(req.full_url, code, msg, hdrs, fp) this site About Us Learn more about Stack Overflow the company Business Learn more about hiring urllib2.httperror: http error 403: forbidden developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is http://stackoverflow.com/questions/8840303/urllib2-http-error-400-bad-request a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up POST request in Python returns urllib2.HTTPError: HTTP Error 400: Bad Request up vote 1 down vote favorite 1 I am trying to create new organizations in Zendesk using their Core API. http://stackoverflow.com/questions/22509946/post-request-in-python-returns-urllib2-httperror-http-error-400-bad-request I have been able to make other calls without issue but this one continues to fail. The following code demonstrates the problem: url = "https://mydomain.zendesk.com/api/v2/organizations.json" new_org = {"organization": {"name": "new organization"}} data = urllib.urlencode(new_org) req = urllib2.Request(url,data) password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, url, 'me@email.com', 'fakepassword') auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_manager) urllib2.install_opener(opener) response = urllib2.urlopen(req) bla = response.read() The following error occurs: Traceback (most recent call last): File "/network/nfshome0/homestore00/me/workspace/Pythony/pythony/test2.py", line 35, in
here for a quick overview of the site Help Center Detailed answers to http://stackoverflow.com/questions/20144366/httperror-http-error-400-bad-request-urllib2 any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn http://www.gossamer-threads.com/lists/python/python/587149 more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community http error Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up HTTPError: HTTP Error 400: Bad request urllib2 up vote 1 down vote favorite 1 I am beginner to python. I am the developer of Easy APIs Project http error 400 (http://gcdc2013-easyapisproject.appspot.com) and was doing a Python implementation of weather API using my project. Visit http://gcdc2013-easyapisproject.appspot.com/APIs_Doc.html to see Weather API. The below is my implementation but it returns HTTPError: HTTP Error 400: Bad request error. import urllib2 def celsius(a): responsex = urllib2.urlopen('http://gcdc2013-easyapisproject.appspot.com/unitconversion?q='+a+' in celsius') htmlx = responsex.read() responsex.close() htmlx = html[1:] #remove first { htmlx = html[:-1] #remove last } htmlx = html.split('}{') #split and put each resutls to array return str(htmlx[1]); print "Enter a city name:", q = raw_input() #get word from user response = urllib2.urlopen('http://gcdc2013-easyapisproject.appspot.com/weather?q='+q) html = response.read() response.close() html = html[1:] #remove first { html = html[:-1] #remove last } html = html.split('}{') #split and put each resutls to array print "Today weather is " + html[1] print "Temperature is " + html[3] print "Temperature is " + celsius(html[3]) Please help me.. python urllib2 share|improve this question asked Nov 22 '13 at 11:56 Isham Mohamed 680520 add a comment|
Post #1 of 3 (1965 views) Permalink 400 Bad Request calling urllib2.urlopen() Hi, I have an url which redirects the client to another url. If I paste the url into a browser, it works fine. If I open it via urllib2.urlopen() I get the following error: Traceback (most recent call last): File "...\crawlRawDocuments.py", line 16, in