python requests.get() returns improperly decoded text instead of UTF-8?

Educated guesses (mentioned above) are probably just a check for Content-Type header as being sent by server (quite misleading use of educated imho). For response header Content-Type: text/html the result is ISO-8859-1 (default for HTML4), regardless any content analysis (ie. default for HTML5 is UTF-8). For response header Content-Type: text/html; charset=utf-8 the result is UTF-8. … Read more

Use python requests to download CSV

This should help: import csv import requests CSV_URL = ‘http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv’ with requests.Session() as s: download = s.get(CSV_URL) decoded_content = download.content.decode(‘utf-8’) cr = csv.reader(decoded_content.splitlines(), delimiter=”,”) my_list = list(cr) for row in my_list: print(row) Ouput sample: [‘street’, ‘city’, ‘zip’, ‘state’, ‘beds’, ‘baths’, ‘sq__ft’, ‘type’, ‘sale_date’, ‘price’, ‘latitude’, ‘longitude’] [‘3526 HIGH ST’, ‘SACRAMENTO’, ‘95838’, ‘CA’, ‘2’, ‘1’, ‘836’, … Read more

Saving response from Requests to file

I believe all the existing answers contain the relevant information, but I would like to summarize. The response object that is returned by requests get and post operations contains two useful attributes: Response attributes response.text – Contains str with the response text. response.content – Contains bytes with the raw response content. You should choose one … Read more

How to prevent python requests from percent encoding my URLs?

It is not good solution but you can use directly string: r = requests.get(url, params=”format=json&key=site:dummy+type:example+group:wheel”) BTW: Code which convert payload to this string payload = { ‘format’: ‘json’, ‘key’: ‘site:dummy+type:example+group:wheel’ } payload_str = “&”.join(“%s=%s” % (k,v) for k,v in payload.items()) # ‘format=json&key=site:dummy+type:example+group:wheel’ r = requests.get(url, params=payload_str) EDIT (2020): You can also use urllib.parse.urlencode(…) with parameter … Read more

requests: how to disable / bypass proxy

The only way I’m currently aware of for disabling proxies entirely is the following: Create a session Set session.trust_env to False Create your request using that session import requests session = requests.Session() session.trust_env = False response = session.get(‘http://www.stackoverflow.com’) This is based on this comment by Lukasa and the (limited) documentation for requests.Session.trust_env. Note: Setting trust_env … Read more

Unable to get local issuer certificate when using requests in python

It’s not recommended to use verify = False in your organization’s environments. This is essentially disabling SSL verification. Sometimes, when you are behind a company proxy, it replaces the certificate chain with the ones of Proxy. Adding the certificates in cacert.pem used by certifi should solve the issue. I had similar issue. Here is what … Read more