Download and save PDF file with Python requests module

You should use response.content in this case: with open(‘/tmp/metadata.pdf’, ‘wb’) as f: f.write(response.content) From the document: You can also access the response body as bytes, for non-text requests: >>> r.content b'[{“repository”:{“open_issues”:0,”url”:”https://github.com/… So that means: response.text return the output as a string object, use it when you’re downloading a text file. Such as HTML file, etc. … Read more

Python requests. 403 Forbidden

It seems the page rejects GET requests that do not identify a User-Agent. I visited the page with a browser (Chrome) and copied the User-Agent header of the GET request (look in the Network tab of the developer tools): import requests url=”http://worldagnetwork.com/” headers = {‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like … Read more

Python requests library how to pass Authorization header with single token

In python: (‘<MY_TOKEN>’) is equivalent to ‘<MY_TOKEN>’ And requests interprets (‘TOK’, ‘<MY_TOKEN>’) As you wanting requests to use Basic Authentication and craft an authorization header like so: ‘VE9LOjxNWV9UT0tFTj4K’ Which is the base64 representation of ‘TOK:<MY_TOKEN>’ To pass your own header you pass in a dictionary like so: r = requests.get(‘<MY_URI>’, headers={‘Authorization’: ‘TOK:<MY_TOKEN>’})

Python Requests library redirect new url

You are looking for the request history. The response.history attribute is a list of responses that led to the final URL, which can be found in response.url. response = requests.get(someurl) if response.history: print(“Request was redirected”) for resp in response.history: print(resp.status_code, resp.url) print(“Final destination:”) print(response.status_code, response.url) else: print(“Request was not redirected”) Demo: >>> import requests >>> … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)