Last active
August 29, 2015 14:24
-
-
Save dsaiztc/ee3e508549f82a9c7eab to your computer and use it in GitHub Desktop.
Python HTTP requests.
http://docs.python-requests.org/en/latest/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
url = 'http://...' | |
r = requests.get(url) | |
r.status_code | |
r.headers | |
r.enconding | |
html_s = r.text # content of the server’s response | |
json_data = json.loads(html_s) | |
json_data = r.json() # requests has its own json decoding | |
r.content # access the response body as bytes, for non-text requests | |
session = requests.Session() # persist certain parameters across requests (like cookies) | |
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36'} | |
r = session.get('https://...', headers=headers) | |
# send some form-encoded data — much like an HTML form | |
payload = {'key1': 'value1', 'key2': 'value2'} | |
r = requests.post("http://httpbin.org/post", data=payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment