TL;DR
--Requests is Python's ** modern ** HTTP library.
--You can make a GET request with requests.get ('URL').
--You can get the response body in text format by setting .text to the response.
Python HTTP library.
Python has a library called urllib2, but as the official website explains Requests is an Apache2 Licensed HTTP library, written in Python, for human beings., It is easy for humans to code.
pip install requests
import requests
There are one-to-one correspondence methods for various HTTP methods.
# GET
requests.get('URL')
# POST
requests.post('URL')
# PUT
requests.put('URL')
# DELETE
requests.delete('URL')
#Get header
requests.head('URL')
Add params = to the argument of the request method with hash of the parameter to be added.
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get('URL', params=payload)
When specifying a UA in a GET request
>>> headers = {'User-Agent': 'Sample Header'}
>>> requests.get('URL', headers=headers)
When adding data to HTTP headers with a POST request
>>> payload = {'send': 'data'}
>>> requests.post('URL', data=json.dumps(payload))
text
Get the returned response body in text format. The request is automatically decoded into unicode.
>>> r = requests.get('http://yahoo.com/')
>>> r.text
'<!DOCTYPE html>\n<html lang="en-US" class="dev-desktop uni-purple-border bkt901 https uni-dark-purple sasb-space" style="">\n<!-- m2 template -->\n<head>\n <meta http-equiv="Content-Type" ...
encoding
Get encoding information
content
Get the response body in binary format.
>>> import requests
>>> r = requests.get('http://www.fnal.gov/faw/designstandards/filesfordownload/FermiLogo_blue.gif')
>>> r.content
Use PIL's Image module.
>>> from PIL import Image
>>> from StringIO import StringIO
>>> r = requests.get('http://www.fnal.gov/faw/designstandards/filesfordownload/FermiLogo_blue.gif')
>>> i = Image.open(StringIO(r.content))
JSON
>>> requests.get('http://ci.nii.ac.jp/ncid/BB08796640.json').json()
{'@context': {'foaf': 'http://xmlns.com/foaf/0.1/', 'prism': 'http://prismstandard.org/namespaces/basic/2.0/', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'owl': 'http://www.w3.org/2002/07/ ...
requests_use.py
# -*- coding: utf-8 -*-
import doctest
import requests
def sample(query):
""" requests sample that use qiita search api
>>> 'title' in sample('python')
True
>>> 'totle' in sample('python')
False
"""
q = {'q': query}
r = requests.get('https://qiita.com/api/v1/search', params=q)
return list(r.json()[0].keys())
if __name__ == "__main__":
doctest.testmod()
References
team information: (housyu, 3cdc8fdc466a6315c030)
Recommended Posts