让 http 服务人类 (一)

1、发送请求

import requests
r = requests.get("https://www.baidu.com/")
print(r)
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
print(r)
"""output:<Response [200]>
"""

2、传递URL参数。如果你想传递 key1=value1 和 key2=value2 到https://www.baidu.com/,Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数。

import requests
payload = {"key1":"value1", "key2":"value2"}
r = requests.get("https://www.baidu.com/", params=payload)
print(r.url)
"""output:https://www.baidu.com/?key1=value1&key2=value2
"""

3 、响应内容

import requests
r = requests.get('https://leetcode-cn.com/')
print(r.text)

4、定制请求头:如果你想为请求添加 HTTP 头部,只要简单地传递一个 dict 给 headers 参数就可以了。

import requests

url = "https://leetcode-cn.com/"
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers = headers)
print(r.headers)

5、发送一些编码为表单形式的数据,要实现这个,只需简单地传递一个字典给 data 参数。你的数据字典在发出请求时会自动编码为表单形式。

import requests

payload = {'key1':'value','key2':'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text)
"""
output: "form": {
"key1": "value",
"key2": "value2"
},
"""

6、检测相应码。

import requests,json

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

#返回相应码
print(r.status_code)

#状态码查询
print(r.status_code==requests.codes.ok)

url = 'http://httpbin.org/status/404'
r = requests.get(url)
print(r.status_code)

#抛出异常
print(r.raise_for_status())
200
True
404
Traceback (most recent call last):
  File "E:/python/practise.py", line 13, in <module>
    print(r.raise_for_status())
  File "E:\python\lib\site-packages\requests\models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: NOT FOUND for url: http://httpbin.org/status/404

 

7、响应头。字典形式

import requests
r = requests.get("https://www.baidu.com/")
print(r.headers["content-type"])
"""
output:text/html
"""

 8、json.dumps()将字典编码成字符串

import requests,json

url = 'https://api.github.com/some/endpoint'
payload={"some":"data"}

#json.dumps()将字典编码成字符串
r = requests.post(url,data=json.dumps(payload))

print(r.status.code)

#使用json直接传参
r = requests.post(url,json = payload)
print(r.raise_for_status())

9、post上传文件。

import requests,json

url = 'http://httpbin.org/post'
file = {"file":open("test.log","rb")}
r=requests.post(url,files = file)
print(r.text)
"""
{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "1598690514911\tgeckodriver\tINFO\tListening on 127.0.0.1:50837\n"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "204", 
    "Content-Type": "multipart/form-data; boundary=2c8ae129f9ccf0e7022223809ec4abaf", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.24.0", 
    "X-Amzn-Trace-Id": "Root=1-5fa00c93-2d70b71b0beae31f71f73349"
  }, 
  "json": null, 
  "origin": "61.152.201.148", 
  "url": "http://httpbin.org/post"
}
"""

10、操作cookies

import requests

url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
print(r.cookies)


url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are = "working")
#发送cookies到服务器
r = requests.get(url,cookies = cookies)
print(r.text)
"""
<RequestsCookieJar[]>
{
  "cookies": {
    "cookies_are": "working"
  }
}
"""

 11、json.load()与json.dump()处理文件

import json
"""
#写入json数据
data={"a":"1"}
with open('data.json',"w") as f:
    json.dump(data,f)
"""

#读取数据
with open("data.json", "r") as f:
    data=json.load(f)
    print(data)

 

posted @ 2020-07-01 22:48  jerrygogo  阅读(180)  评论(0编辑  收藏  举报