If the response is in json you could do something like (python3):
Copyimport json
import requests as reqs
# Make the HTTP request.
response = reqs.get('https://demo.ckan.org/api/3/action/group_list')
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.text)
for i in response_dict:
print("key: ", i, "val: ", response_dict[i])
To see everything in the response you can use .__dict__:
Copyprint(response.__dict__)
Edit in May 2024 to add a suggestion on how to address if objects in the response dict is not JSON serializable.
Copyimport json
...
print(json.dumps(response.text, indent=4, sort_keys=True, default=lambda o:'<not serializable>'))
Answer from Jortega on Stack OverflowIf the response is in json you could do something like (python3):
Copyimport json
import requests as reqs
# Make the HTTP request.
response = reqs.get('https://demo.ckan.org/api/3/action/group_list')
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.text)
for i in response_dict:
print("key: ", i, "val: ", response_dict[i])
To see everything in the response you can use .__dict__:
Copyprint(response.__dict__)
Edit in May 2024 to add a suggestion on how to address if objects in the response dict is not JSON serializable.
Copyimport json
...
print(json.dumps(response.text, indent=4, sort_keys=True, default=lambda o:'<not serializable>'))
If you push, for example image, to some API and want the result address(response) back you could do:
Copyimport requests
url = 'https://uguu.se/api.php?d=upload-tool'
data = {"name": filename}
files = {'file': open(full_file_path, 'rb')}
response = requests.post(url, data=data, files=files)
current_url = response.text
print(response.text)
When should I use browser automation instead of requests?
Use browser automation tools like Playwright or Selenium when you need to load JavaScript, handle clicks, or when you're blocked by TLS fingerprinting. While these tools solve the TLS problem, they are slower and use more computer resources. ScrapFly offers both simple requests and full browser automation.
How do I rotate headers to avoid detection?
Maintain a list of realistic header sets and randomly select one per request. Vary the User-Agent, Accept-Language, and Referer values. Combine this with proxy rotation for better results.
Why do my headers work in testing but fail in production?
This is usually because you're sending too many requests. In testing, your low number of requests doesn't trigger extra anti-bot checks. In production, more requests trigger TLS fingerprinting. Even with perfect headers, a different TLS fingerprint will get you blocked. ScrapFly solves this by using real browser TLS profiles.
Videos
https://www.youtube.com/watch?v=tb8gHvYlCFs&t=374s
I am watching this vid, all excited to learn requests, but I don't really understand nearly anything, like what is r.content doing, or the r.json() function does I also don't get what
what is in r.content, it returns things, but I don't really understand what these things are, r.text returns a dictionary of args, headers, origin etc but I don't really get how this information is useful.
r.get, r.post, or r.put really do I am really sorry for this but if someone could link an article or a little video that explains what the content is, like I am not sure what to search for.
What are the pros and cons?
Examples:
r = req.get('https://picsum.photos/200/300', stream=True) with open(f'file.png', 'wb') as f: shutil.copyfileobj(r.raw, f) or
r = req.get('https://picsum.photos/200/300') with open(f'file.png', 'wb') as f: f.write(r.content)