>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
import requests
URL="http://www.bsdmap.com/"
r = requests.get(URL)
r = requests.post(URL)
r = requests.put(URL)
r = requests.delete(URL)
r = requests.head(URL)
r = requests.options(URL)
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print r.url
u'http://httpbin.org/get?key2=value2&key1=value1'
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
'[{"repository":{"open_issues":0,"url":"https://github.com/...
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1'
You can also access the response body as bytes, for non-text requests:
>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
The gzip and deflate transfer-encodings are automatically decoded for you.
For example, to create an image from binary data returned by a request,
ou can use the following code:
>>> from PIL import Image
>>> from StringIO import StringIO
>>> i = Image.open(StringIO(r.content))
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
>>> requests.get('http://github.com', timeout=0.001)
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> headers = {'content-type': 'application/json'}
>>> r = requests.post(url, data=json.dumps(payload), headers=headers)
d=pq("hello ")
d=pq(filename=path_to_html_file)
d=pq(url='http://www.baidu.com')
p=pq("hello ")
p('head').html()#返回hello
p('head').text()#返回hello
d=pq('test 1
test 2
')
d('p')#返回[,
]
print d('p')#返回
test 1
test 2
print d('p').html()#返回test 1
print d('p').eq(1).html() #返回test 2
d=pq("test 1
test 2
")
d('p').filter('#1') #返回[]
d('p').filter('.2') #返回[
]
d=pq("test 1
test 2
")
d('div').find('p')#返回[,
]
d('div').find('p').eq(0)#返回[]
d=pq("test 1
test 2
")
d('#1').html()#返回test 1
d('.2').html()#返回test 2
d=pq("")
d('a').attr('href')#返回http://hello.com
d('p').attr('id')#返回my_id
d('a').attr('href', 'http://baidu.com')
d=pq('')
d.addClass('my_class')#返回[]
d=pq("")
d.hasClass('my_class')#返回True
d=pq("hello
world
")
d.children()#返回[,
]
d.children('#2')#返回[
]
d=pq("hello
world
")
d('p').parents()#返回[]
d('#1').parents('span')#返回[]
d('#1').parents('p')#返回[]
hello
world
d('p:first').nextAll()#返回[,
]
d('p:last').nextAll()#返回[
]
d=pq("test 1
test 2
")
d('p').not_('#2')#返回[]