python requests库

 资料来自:Requests库常用方法及参数介绍 (biancheng.net)

python requests包的request()函数中的参数-params和data的区别介绍 - 腾讯云开发者社区-腾讯云 (tencent.com)

目录

安装

申请

args参数

1. params: 字典或者字节序列,作为参数增加到url中

2.data:字典、字节、或文件对象,作为request

 3.json:JSON格式的数据,作为request的内容(使用方式和data一致,不再演示)

4.headers: 字典,常用有user-agent

5.timeout:该参数可以传入一个简单的浮点数,它将请求的连接部分和读取部分设为相同的超时时间

 6.auth:该参数的支持用户认证功能,也就是适合那些需要验证用户名、密码的网站。auth 的参数形式是一个元组

7.files: 字典类型,传输文件

8.proxies: 字典类型,设定访问代理服务器,可以增加登录认证

异常

功能目的实现

获取text内容

获取json内容

获取二进制内容


requests 是python的第三方http库

安装

win+r后cmd 输入pip install requests

导入requests包

import requesets

申请

requests.get(url)
requests.post(url)
requests.head(url)
requests.put(url)

get是请求页面信息返回源代码。

post是请求服务器接受所指定的文档作为所标识的url的新的从属实体

head是返回页面的头信息

put是从客户端传送到服务器端取代指定的文档的内容

args参数

1. params: 字典或者字节序列,作为参数增加到url中

返回的url为http://www.baidu.cpm/s?参数1=content_one&参数2=content_two

import requests
url = 'http://www.baidu.com/s?'
param = {
         '参数1':'content_one',
         '参数2':'content_two"}
r=requests.get(url=url,param =param)
print(r.url)  
print(r.text)  

2.data:字典、字节、或文件对象,作为request

import requests
kv={“key1”:“value1”,“key2”:“value2”}
r=requests.post(“http://httpbin.org/post”,data=kv)
print(r.text)

 运行结果:

{
“args”: {},
“data”: “”,
“files”: {},
“form”: {
“key1”: “value1”,
“key2”: “value2”
},
“headers”: {
“Accept”: “/”,
“Accept-Encoding”: “gzip, deflate”,
“Connection”: “close”,
“Content-Length”: “23”,
“Content-Type”: “application/x-www-form-urlencoded”,
“Host”: “httpbin.org”,
“User-Agent”: “python-requests/2.18.1”
},
“json”: null,
“origin”: “113.235.118.39”,
“url”: “http://httpbin.org/post”
}

 3.json:JSON格式的数据,作为request的内容(使用方式和data一致,不再演示)

4.headers: 字典,常用有user-agent

import requests
headers = {
            'user-agent'='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42'
            }
response = requests.get('http://www.baidu.com/s',headers=headers)
print(response.text)

5.timeout:该参数可以传入一个简单的浮点数,它将请求的连接部分和读取部分设为相同的超时时间

import requests
response = requests.get('http://www.baidu.com',timeout=0.5)
print(response.status_code)
#如果0.5s没有相应则会抛出异常
# requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='http://www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(, 'Connection to gethub.com timed out. (connect timeout=1)'))

 6.auth:该参数的支持用户认证功能,也就是适合那些需要验证用户名、密码的网站。auth 的参数形式是一个元组

import requests
auth =('userName','password')
response = requests.get('http://www.baidu.com',auth = auth)

7.files: 字典类型,传输文件

import requests
files = {
        'file':open('cookie.txt','rb')
        }
response = requests.post('http://httpbin.org/post', files=files)
print(response.text)

返回值

{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "#LWP-Cookies-2.0\r\nSet-Cookie3: BAIDUID=\"D2B4E137DE67E271D87F03A8A15DC459:FG=1\"; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; expires=\"2086-02-13 11:15:12Z\"; version=0\r\nSet-Cookie3: BIDUPSID=D2B4E137DE67E271D87F03A8A15DC459; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; expires=\"2086-02-13 11:15:12Z\"; version=0\r\nSet-Cookie3: H_PS_PSSID=25641_1465_21087_17001_22159; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; discard; version=0\r\nSet-Cookie3: PSTM=1516953672; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; expires=\"2086-02-13 11:15:12Z\"; version=0\r\nSet-Cookie3: BDSVRTM=0; path=\"/\"; domain=\"百度一下,你就知道\"; path_spec; discard; version=0\r\nSet-Cookie3: BD_HOME=0; path=\"/\"; domain=\"百度一下,你就知道\"; path_spec; discard; version=0\r\n"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "909", 
    "Content-Type": "multipart/form-data; boundary=84835f570cfa44da8f4a062b097cad49", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "json": null, 
  "origin": "183.64.61.29", 
  "url": "http://httpbin.org/post"
}{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "#LWP-Cookies-2.0\r\nSet-Cookie3: BAIDUID=\"D2B4E137DE67E271D87F03A8A15DC459:FG=1\"; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; expires=\"2086-02-13 11:15:12Z\"; version=0\r\nSet-Cookie3: BIDUPSID=D2B4E137DE67E271D87F03A8A15DC459; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; expires=\"2086-02-13 11:15:12Z\"; version=0\r\nSet-Cookie3: H_PS_PSSID=25641_1465_21087_17001_22159; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; discard; version=0\r\nSet-Cookie3: PSTM=1516953672; path=\"/\"; domain=\".baidu.com\"; path_spec; domain_dot; expires=\"2086-02-13 11:15:12Z\"; version=0\r\nSet-Cookie3: BDSVRTM=0; path=\"/\"; domain=\"百度一下,你就知道\"; path_spec; discard; version=0\r\nSet-Cookie3: BD_HOME=0; path=\"/\"; domain=\"百度一下,你就知道\"; path_spec; discard; version=0\r\n"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "909", 
    "Content-Type": "multipart/form-data; boundary=84835f570cfa44da8f4a062b097cad49", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "json": null, 
  "origin": "183.64.61.29", 
  "url": "http://httpbin.org/post"
}

8.proxies: 字典类型,设定访问代理服务器,可以增加登录认证

import requests
proxies = {
            'http':'http://127.0.0.1:9743',
            'https':'https://127.0.0.1:9743,
            }
response = requests.get("https://www.taobao.com",proxies = proxies)
print(response.status_code)

异常

以下是request.exceptions下的各种异常错误
RequestException:
HTTPError(RequestException)  
UnrewindableBodyError(RequestException)  
RetryError(RequestException)  
ConnectionError(RequestException) ProxyError(ConnectionError)
SSLError(ConnectionError)
ConnectTimeout(ConnectionError, Timeout)
Timeout(RequestException) ReadTimeout
URLRequired(RequestException)  
TooManyRedirects(RequestException)  
MissingSchema(RequestException, ValueError)  
InvalidSchema(RequestException,ValueError)  
InvalidURL(RequestException,ValueError)  
InvalidHeader(RequestException,ValueError)  
ChunkedEncodingError(RequestException)  
StreamConsumedError(RequestException,TypeError)  
ContentDecodingError(RequestException,BaseHTTPError)

所有Requests显式抛出的异常都继承自 requests.exceptions.RequestException
————————————————
版权声明:本文为CSDN博主「JermeryBesian」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Urbanears/article/details/79288972

功能目的实现

获取text内容

import requests
response = requests.get(url,param,headers)
print(response.text)
response.close()

获取json内容

import requests
response = requests.get(url,param,headers)
print(response.json()[''])
response.close()

获取二进制内容

import requests
response = requests.get(url,param,headers)
print(response.content)
response.close()

你可能感兴趣的:(python,开发语言)