requests基础总结

1.需要导入requests包

import requests

2.get的使用

# final_url = "http://gamefunc.cc.163.com/integral/acu_billboard?uid=185311124&month=&page=1&size=10&game=7
# 获取json数据
url = "http://gamefunc.cc.163.com/integral/acu_billboard?"

# 添加参数
params = {'uid': 185311124, 'month': '', 'page': 1, 'size': 10, 'game': 7}
r = requests.get(url=url, params=params)
print(r.url)

# 返回的结果类型
print(type(r))
# 返回的结果的状态码
print(r.status_code)
# 编码方式
print(r.encoding)
# 响应的Cookies内容
print(r.cookies)
# 响应的内容,如一个html文件或者直接cookie内容
print(r.text)
# 利用 json() 方法解析,比如将utf-8数据转成Unicode
print(r.json())

输出结果:

http://gamefunc.cc.163.com/integral/acu_billboard?size=10&month=&uid=185311124&game=7&page=1

200
UTF-8

{"reason": "Success", "code": 0, "data": {"revenue_score": 0, "is_official": 0, "guild_rank": 0, "last_month": "2018-08", "gift_score": 0, "cur_month": "2018-09", "in_wlist": 0, "rank": 0, "num": 0, "billboard": [{"status": 3, "topcid": 5260583, "uid": 136701777, "head_url": "http://cc.fp.ps.netease.com/file/5a9b48b6aa49f12552782db93Wz8USyM", "subcid": 5313383, "nick": "\u3010\u8352\u91ce183\u3011\u53f8\u4ee4 \u4e0d\u5fd8\u521d\u5fc3", "num": 451, "roomid": 183, "ccid": 240299729}, {"status": 1, "topcid": 0, "uid": 248357642, "head_url": "http://cc.res.netease.com/webcc/portrait/nsep/headicon/builtin/38", "subcid": 0, "nick": "cc\u99a8\u513f", "num": 396, "roomid": 0, "ccid": 326455965}, {"status": 1, "topcid": 0, "uid": 207464170, "head_url": "http://cc.fp.ps.netease.com/file/5b50c8df8b74274d91021a95FeTueVys", "subcid": 0, "nick": "\u5fae\u5a31-\u827e\u57ce\u4e3b", "num": 353, "roomid": 0, "ccid": 272325988}, {"status": 1, "topcid": 0, "uid": 208440838, "head_url": "http://cc.fp.ps.netease.com/file/5adb35d86f0494dd5b3034d2cMmzO7fi", "subcid": 0, "nick": "\u5fae\u5a31-\u5e05\u5230\u70b8\u5b87\u5b99", "num": 348, "roomid": 0, "ccid": 273488066}, {"status": 1, "topcid": 0, "uid": 101216346, "head_url": "http://cc.fp.ps.netease.com/file/5affd799143cfa16323c2435WvzY6rQR", "subcid": 0, "nick": "\u301058555\u3011\u9177\u9177\u7684\u8001\u56db", "num": 335, "roomid": 0, "ccid": 127956484}, {"status": 1, "topcid": 0, "uid": 200331439, "head_url": "http://cc.fp.ps.netease.com/file/5b7cafd86f049425f4c421180aj2oouk", "subcid": 0, "nick": "\u3010\u8352\u91ce183\u3011 \u5751\u54e5", "num": 326, "roomid": 0, "ccid": 263548132}, {"status": 1, "topcid": 0, "uid": 203546860, "head_url": "http://cc.fp.ps.netease.com/file/5a8d1314a7f2522d20367160zbmy3iY3", "subcid": 0, "nick": "9166\u309e\u8089\u997c\u3010\u672c\u6708\u968f\u7f18\u64ad\u3011", "num": 319, "roomid": 0, "ccid": 267481293}, {"status": 4, "topcid": 5260583, "uid": 192317854, "head_url": "http://cc.fp.ps.netease.com/file/5b69d2678b74278cacc6facdXA5epHFT", "subcid": 5297328, "nick": "\u3010\u8352\u91ce183\u3011\u5c0f\u5634\u5f88\u751c", "num": 307, "roomid": 183, "ccid": 253532271}, {"status": 4, "topcid": 4862438, "uid": 248719094, "head_url": "http://cc.fp.ps.netease.com/file/5b8756eca7f2521c6c87999azz3GWyVy", "subcid": 5648852, "nick": "Cc\u76f4\u64adYoYo", "num": 306, "roomid": 2121, "ccid": 326876740}, {"status": 1, "topcid": 0, "uid": 202665175, "head_url": "http://cc.fp.ps.netease.com/file/5b4c6d4696dee41e8f3f9591YwzhedtV", "subcid": 0, "nick": "22155\u4e36\u5927\u524d\u95e8", "num": 304, "roomid": 0, "ccid": 266344933}], "acu_score": 0, "total": 1917, "page": 1, "size": 10}}
{'reason': 'Success', 'code': 0, 'data': {'page': 1, 'gift_score': 0, 'is_official': 0, 'billboard': [{'roomid': 183, 'status': 3, 'head_url': 'http://cc.fp.ps.netease.com/file/5a9b48b6aa49f12552782db93Wz8USyM', 'ccid': 240299729, 'num': 451, 'nick': '【荒野183】司令 不忘初心', 'uid': 136701777, 'topcid': 5260583, 'subcid': 5313383}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.res.netease.com/webcc/portrait/nsep/headicon/builtin/38', 'ccid': 326455965, 'num': 396, 'nick': 'cc馨儿', 'uid': 248357642, 'topcid': 0, 'subcid': 0}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.fp.ps.netease.com/file/5b50c8df8b74274d91021a95FeTueVys', 'ccid': 272325988, 'num': 353, 'nick': '微娱-艾城主', 'uid': 207464170, 'topcid': 0, 'subcid': 0}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.fp.ps.netease.com/file/5adb35d86f0494dd5b3034d2cMmzO7fi', 'ccid': 273488066, 'num': 348, 'nick': '微娱-帅到炸宇宙', 'uid': 208440838, 'topcid': 0, 'subcid': 0}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.fp.ps.netease.com/file/5affd799143cfa16323c2435WvzY6rQR', 'ccid': 127956484, 'num': 335, 'nick': '【58555】酷酷的老四', 'uid': 101216346, 'topcid': 0, 'subcid': 0}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.fp.ps.netease.com/file/5b7cafd86f049425f4c421180aj2oouk', 'ccid': 263548132, 'num': 326, 'nick': '【荒野183】 坑哥', 'uid': 200331439, 'topcid': 0, 'subcid': 0}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.fp.ps.netease.com/file/5a8d1314a7f2522d20367160zbmy3iY3', 'ccid': 267481293, 'num': 319, 'nick': '9166ゞ肉饼【本月随缘播】', 'uid': 203546860, 'topcid': 0, 'subcid': 0}, {'roomid': 183, 'status': 4, 'head_url': 'http://cc.fp.ps.netease.com/file/5b69d2678b74278cacc6facdXA5epHFT', 'ccid': 253532271, 'num': 307, 'nick': '【荒野183】小嘴很甜', 'uid': 192317854, 'topcid': 5260583, 'subcid': 5297328}, {'roomid': 2121, 'status': 4, 'head_url': 'http://cc.fp.ps.netease.com/file/5b8756eca7f2521c6c87999azz3GWyVy', 'ccid': 326876740, 'num': 306, 'nick': 'Cc直播YoYo', 'uid': 248719094, 'topcid': 4862438, 'subcid': 5648852}, {'roomid': 0, 'status': 1, 'head_url': 'http://cc.fp.ps.netease.com/file/5b4c6d4696dee41e8f3f9591YwzhedtV', 'ccid': 266344933, 'num': 304, 'nick': '22155丶大前门', 'uid': 202665175, 'topcid': 0, 'subcid': 0}], 'last_month': '2018-08', 'rank': 0, 'total': 1917, 'size': 10, 'acu_score': 0, 'cur_month': '2018-09', 'revenue_score': 0, 'in_wlist': 0, 'guild_rank': 0, 'num': 0}}

3.获取来自服务器的原始套接字响应,需要在初始请求中设置 stream=True

r = requests.get("https://github.com/timeline.json", stream=True)
print(r.raw)
print(r.raw.read(100))

输出结果


b'{"message":"Hello there, wayfaring stranger. If you\xe2\x80\x99re reading this then you probably didn\xe2\x80\x99t see'

4.get请求添加headers和链接带参数

url = "https://www.baidu.com/"
# 添加headers
headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36'}
# 请求链接添加参数
payload = {'wd': '学生', 'tn': 'baiduhome_pg'}
r = requests.get(url=url, params=payload, headers=headers)
print(r.url)
print(r.content)

输出结果

https://www.baidu.com/?tn=baiduhome_pg&wd=%E5%AD%A6%E7%94%9F
b'\xef\xbb\xbf\n

输出结果

{"sessionUuid":"translate_uuid1538147300759","translate":{"errCode":0,"errMsg":"","sessionUuid":"translate_uuid1538147300759","source":"zh","target":"en","records":[{"sourceText":"字典","targetText":"Dictionaries","traceId":"02534dd0bd7d4720ab1a7186ee82ab8d"}],"full":true,"options":{}},"dict":null,"suggest":null,"errCode":0,"errMsg":"ok"}
https://fanyi.qq.com/api/translate
  • 传JSON格式的数据,可以用 json.dumps() 方法把表单数据序列化
import json

url = 'http://httpbin.org/post'
payload = {'some': 'data', 'name': 'xiaoba', 'id': 2342342}
r = requests.post(url, data=json.dumps(payload))
print(r.text)

输出结果

{
  "args": {}, 
  "data": "{\"id\": 2342342, \"some\": \"data\", \"name\": \"xiaoba\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "49", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.19.1"
  }, 
  "json": {
    "id": 2342342, 
    "name": "xiaoba", 
    "some": "data"
  }, 
  "origin": "223.104.64.47", 
  "url": "http://httpbin.org/post"
}
  • 用 file 参数上传文件
url = 'http://httpbin.org/post'
files = {'file': open('test.txt', 'rb')}
r = requests.post(url, files=files)
print(r.text)

输出结果

{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "Hello,I'm a women."
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "162", 
    "Content-Type": "multipart/form-data; boundary=39856e08a2727e642ad4dd0862129a08", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.19.1"
  }, 
  "json": null, 
  "origin": "223.104.64.47", 
  "url": "http://httpbin.org/post"
}
  • requests 支持流式上传数据,该方式允许发送大的数据流或文件而无需先把它们读入内存,类似方式如下:
with open('test.txt', 'r') as f:
    r = requests.post('http://some.url/streamed', data=f)

6.获取cookie

url = 'https://www.baidu.com'
r = requests.get(url)
print(r.cookies)
for item in r.cookies:
    print(item.name, item.value)

7.利用 cookies 变量来向服务器发送 cookies 信息

url = 'http://httpbin.org/cookies'
cookies = dict(uid='wfdfewwe',name='xiaoba')
print(cookies)
r = requests.get(url=url, cookies=cookies)
print(r.text)

输出结果

{'name': 'xiaoba', 'uid': 'wfdfewwe'}
{
  "cookies": {
    "name": "xiaoba", 
    "uid": "wfdfewwe"
  }
}

8.SSL证书验证:要想检查某个主机的SSL证书,可以使用 verify 参数

r = requests.get('https://github.com', verify=True)
print r.text

想跳过刚才 12306 的证书验证,把 verify 设置为 False 即可

9.代理
可以通过为任意请求方法提供 proxies 参数来配置单个请求

proxies = {
  "http": "118.190.95.35:9001"
}
r = requests.post("https://www.baidu.com/", proxies=proxies)
print(r.text)
西刺代理:http://www.xicidaili.com/

你可能感兴趣的:(requests基础总结)