虫虫

Python requests库(一)

2019-12-07  本文已影响0人  伊洛的小屋

本文首发自伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!

1.Requests 库介绍
2.安装方式
 pip install requests
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
3.简单的例子
➜  ~ source yiluo/bin/activate
(yiluo) ➜  ~ python
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests
>>> r =requests.get('https://www.jianshu.com/')
>>> print(r.status_code)
403
>>>

很不幸返回的状态码不是 200

>>> import requests
>>> headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
>>> r = requests.get('https://www.jianshu.com', headers = headers)
>>> print(r.status_code)
200

这次请求成功了,上面代码中 rResponse对象,可以在这个对象中获取到我们想要的信息, 我们再输出些其他的响应内容看下

>>> import requests
>>> headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
>>> r = requests.get('https://www.jianshu.com', headers = headers)
>>> print(type(r))
<class 'requests.models.Response'>
>>> print(type(r.text))
<class 'str'>
>>> print(r.cookies)
<RequestsCookieJar[<Cookie locale=zh-CN for www.jianshu.com/>]>

如上输出了响应的类型响应体类型响应的cookies

>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})
>>> r = requests.delete('http://httpbin.org/delete')
>>> r = requests.head('http://httpbin.org/get')
>>> r = requests.options('http://httpbin.org/get')
4.经常使用的请求
>>> import requests
>>> r = requests.get('http://httpbin.org/get')
>>> print(r.text)
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0"
  },
  "origin": "116.25.147.198, 116.25.147.198",
  "url": "https://httpbin.org/get"
}
>>> import requests
>>> data = {'name':'Tom', 'age':18}
>>> r = requests.get("http://httpbin.org/get", params=data)
>>> print(r.text)
{
  "args": {
    "age": "18",
    "name": "Tom"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0"
  },
  "origin": "116.25.147.198, 116.25.147.198",
  "url": "https://httpbin.org/get?name=Tom&age=18"
}
>>> import requests
>>> data = {'name':'Tom', 'age':'18'}
>>> r = requests.post('http://httpbin.org/post', data = data)
>>> print(r.text)
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "age": "18",
    "name": "Tom"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "15",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.22.0"
  },
  "json": null,
  "origin": "116.25.147.198, 116.25.147.198",
  "url": "https://httpbin.org/post"
}
5.具体的一些使用方式
>>> import requests
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2
>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r.json()
>>> import requests
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)
......待续

享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读