Python facebook feeder http error - python

My code :
def posting(self , facebook_id , message):
access_token = "my access_token"
post_data = {'access_token':access_token, 'message':message}
request_path = str(facebook_id)+'/feed'
post_data = urllib.urlencode(post_data)
response = urllib2.urlopen('https://graph.facebook.com/%s' % request_path, post_data)
print "posting is successfull !! :D"
and the error :
response = urllib2.urlopen('https://graph.facebook.com/%s' % request_path, post_data)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
what is the problem ?
this code is working couple months ago ?

Related

while reading json data from url using python gives error "urllib.error.HTTPError: HTTP Error 403: Forbidden"

with this code I am reading a URL and using the data for filtration but urllib could not work
url = "myurl"
response = urllib.request.urlopen(url)
data = json.loads(response.read())
yesterday it was working well but now giving me error:
Traceback (most recent call last):
File "vaccine_survey.py", line 22, in <module>
response = urllib.request.urlopen(url)
File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
this works for me, here 'myurl' is a url address
from urllib.request import Request, urlopen
req = Request('myurl', headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(req).read()
data = json.loads(response.read())

Http Error 302 in python

So while using urllib I get this error, here is the full traceback:
Traceback (most recent call last):
File "C:\Users\user\Desktop\Dads project\Word search tool.py", line
148, in <module>
print_absolute_counts(line.strip('\n'), 'english', smoothing=0,
start_year=1799, end_year=1801)
File "C:\Users\user\Desktop\project\Word search tool.py", line 43, in
print_absolute_counts
absolute_counts = retrieve_absolute_counts(token, corpus, smoothing,
start_year, end_year)
File "C:\Users\user\Desktop\Dads project\Word search tool.py", line 85, in
retrieve_absolute_counts
page = urllib.request.urlopen(urllib.request.Request(url)).read()
File "C:\Users\user\AppData\Local\Programs\Python\Python37-
32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-
32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-
32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 755, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 563, in error
result = self._call_chain(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 745, in http_error_302
self.inf_msg + msg, headers, fp)
urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
I want to know if their is a way to fix this, it comes as I search something in a website and it returns this, other searches appear to work perfectly, but this one specific search being: aaliis returns an error, its a sort of word search for the google ngrams.
HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
Http response code 302 is for redirecting. In your case it seems like the url you requested responds with 302 too many times, so the urllib raises exception to prevent you from being stuck in infinite loop of redirections.
Probably the url you request requires some cookies and redirects you if they're not set. Add cookies to your request or use better libs that do this by default: http://docs.python-requests.org/en/master/

I can not scrape a webpage with Beautiful Soup

I'm trying to scrap https://www.crowdcube.com/investments?sector=technology with BeautifulSoup in Python 3.
Traceback (most recent call last):
File "D:\DataVisualization\lib\urllib\request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "D:\DataVisualization\lib\urllib\request.py", line 472, in open
response = meth(req, response)
File "D:\DataVisualization\lib\urllib\request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "D:\DataVisualization\lib\urllib\request.py", line 510, in error
return self._call_chain(*args)
File "D:\DataVisualization\lib\urllib\request.py", line 444, in _call_chain
result = func(*args)
File "D:\DataVisualization\lib\urllib\request.py", line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
use requests and this site do not need UA:
In [23]: import requests
In [24]: r = requests.get('https://www.crowdcube.com/investments?sector=technology')
In [25]: r.status_code
Out[25]: 200

python jenkins api quiet_down() not working

I am trying to send quiet_down() using python-jenkins api but I get the following messages and the server doesn't go into shutdown mode.
import jenkins
server = jenkins.Jenkins('serveraddress', username='someuser', password='somepassword')
server.quiet_down()
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\jenkins__init__.py", line 1642, in quiet_
down
self.jenkins_open(request)
File "C:\Python27\lib\site-packages\jenkins__init__.py", line 431, in jenkins
_open
response = urlopen(req, timeout=self.timeout).read()
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 435, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 473, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 405: Method Not Allowed
I have checked and I'm being authenticated correctly and I do have all the rights on it. Jenkins version is 2.7.4.
Any help would be appreciated.
Thanks.

Http 404 error when uing Gitlab API to add SSH key

I have a valid user_id, and also the admin token (which is stored in GITLAB_ADMIN_TOKEN)
But when I run following Python to add the SSH key for user id, I get a 404 error.
# Add SSH Key
url = 'http://114.xx.xx.xx:8080/api/v3/users/' + str(user_id)+ '/keys?private_token=' + GITLAB_ADMIN_TOKEN
print url
values = {'id':user_id,
'title':'mykey',
'key':'ssh-rsa xxxxxxxxxxxxxxxxx ..... root#ubuntu'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
The result is:
http://114.xx.xx.xx:8080/api/v3/users/30/keys?private_token=xxxxxx
Traceback (most recent call last):
File "testgitlab.py", line 61, in
response = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
I know where is the problem now.
In my original implementation, I pasted the key with
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrTAR1MVs1juu1MuZ8C09CZpxm56hDzguRGuenmHez7Co9NQyx3GKLa5ksfdMNk+OBLVuf/fFZZ1ZoFGH9Cz/xNkxwtzjd6UiTt/6ECO9rClYK3LfX5RTv7a2O9zxhsudpofhIkUS0fYFmRlTi/htssbU5IC+U1i+xXHvfBChdLd2EasakGB89+Sw5t74cVyMiC8mRkcLxpCRI1BPEQd5FRZQr8piQxW2APcWnT7h18gY1F9qm50pq2PQgk7rQtkLMQKVVu30/95W7IBVfTMfklnDk3z0Dj4EcqzKYeeenwVn6YdC3fI5ZmLTpNhLlLwJPlBDQnUSIn8pBrmXfpPy7 root#ubuntu
But the root#ubuntu shouldn't be included. When I removed it from my 2048-bit key, it works now.

Categories