requests

安装

pip install requests

请求

.get()

params参数字典表

import requests
url = 'http://httpbin.org/get'
r = requests.get(url)
r.status_code
200

r.text
'{\n  "args": {}, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Connection": "close", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.18.4"\n  }, \n  "origin": "175.167.154.93", \n  "url": "http://httpbin.org/get"\n}\n'

d = {'name':'Mace', 'age':20}
r = requests.get(url, params=d)
r.text
'{\n  "args": {\n    "age": "20", \n    "name": "Mace"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Connection": "close", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.18.4"\n  }, \n  "origin": "175.167.154.93", \n  "url": "http://httpbin.org/get?name=Mace&age=20"\n}\n'

r.url
'http://httpbin.org/get?name=Mace&age=20'

.post()

data数据字典表
json要提交的json数据

url = 'http://httpbin.org/post'
d
{'name': 'Mace', 'age': 20}
r = requests.post(url, data=d)
r.text
'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "age": "20", \n    "name": "Mace"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Connection": "close", \n    "Content-Length": "16", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.18.4"\n  }, \n  "json": null, \n  "origin": "175.167.154.93", \n  "url": "http://httpbin.org/post"\n}\n'

响应

类型 requests.models.Response

  • .status_code 状态码
  • .ok 是否成功
  • .encoding 编码
  • .apparent_encoding 表现编码
  • .content 内容文本
  • .text 内容文本
  • .json() 内容json
  • .history 重定向历史
import requests
url = 'http://httpbin.org/get'
d = {'name':'mace', 'age':20}
r = requests.get(url, params=d)
type(r)
<class 'requests.models.Response'>
r.status_code
200
r.ok
True
r.encoding
r.apparent_encoding
'ascii'
r.text
'{\n  "args": {\n    "age": "20", \n    "name": "mace"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Connection": "close", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.18.4"\n  }, \n  "origin": "175.167.154.93", \n  "url": "http://httpbin.org/get?name=mace&age=20"\n}\n'
r.json()
{'args': {'age': '20', 'name': 'mace'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '175.167.154.93', 'url': 'http://httpbin.org/get?name=mace&age=20'}

自定义 headers

.get(url, headers={'key':'value'})

import requests
url = 'http://httpbin.org/get'
r = requests.get(url)
print(r.text)
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "origin": "175.167.154.93", 
  "url": "http://httpbin.org/get"
}
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}
r = requests.get(url, headers=headers)
print(r.text)
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
  }, 
  "origin": "175.167.154.93", 
  "url": "http://httpbin.org/get"
}
headers['Referer'] = 'httpbin.org'
headers
{'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36', 'Referer': 'httpbin.org'}
r = requests.get(url, headers=headers)
print(r.text)
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Referer": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
  }, 
  "origin": "175.167.154.93", 
  "url": "http://httpbin.org/get"
}

cookies 操作

  • jar = requests.cookies.RequestsCookieJar()
  • jar.set('key', 'value')
  • requests.get(url, cookies=jar)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 如果你把上篇多线程和多进程的文章搞定了,那么要恭喜你了 。你编写爬虫的能力上了一个崭新的台阶。不过,我们还不能沾沾...
    猴哥爱读书阅读 8,691评论 2 31
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,828评论 19 139
  • 目录: Python网络爬虫(一)- 入门基础Python网络爬虫(二)- urllib爬虫案例Python网络爬...
    一只写程序的猿阅读 14,427评论 17 68
  • 窗外有一棵树。微凉的秋风拂过树叶,发出一阵清脆的哗哗声,几片叶子落下来,随着节奏打起了旋,像极了蝶。 ...
    周子游_阅读 2,817评论 0 2
  • 好想再次在电梯里 用我的Cafe遭遇你的衣角 惊醒你的微笑 好想深情望着 你硕大迷人的眼睛 描绘出 与你漫步于正义...
    Wang_无处安放的青春阅读 1,751评论 0 0

友情链接更多精彩内容