python请求库

urllib库

python原生基本库

requests库

安装 pip install requests

它是在urllib3基础上更加强大的请求库

实例引入

import requests
response = requests.get("https://www.baidu.com")
print(response.status_code)
print(response.text)
print(response.content)
print(response.cookies)
print(response.url)

各种请求方式

import requests

requests.post("http://httpbin.org/post")
requests.put("http://httpbin.org/put")
requests.head("http://httpbin.org/get")
requests.delete("http://httpbin.org/delete")
requests.options("http://httpbin.org/get")

带参数的get请求

import requests
response = requests.get("http://httpbin.org/get")
print(response.text)
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.21.0",
    "X-Amzn-Trace-Id": "Root=1-5e5a5e25-0029a7ce338de2665befc666"
  },
  "origin": "183.230.193.190",
  "url": "http://httpbin.org/get"
}

?

import requests

data = {
    "name":"germey",
    "age":"22"
}
response = requests.get("http://httpbin.org", params = data)  # ==> http://httpbin.org/?name=germey&age=22
print(response.url)
print(response.text)

添加headers

import requests

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
response = requests.get("https://www.bilibili.com", headers=headers)
print(response.text)

基本post请求

import requests

data = {
    "name":"germey",
    "age":"22"
}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
response = requests.post("https://httpbin.org/post", data = data, headers = headers)
print(response.url)
print(response.text)
https://httpbin.org/post
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "age": "22",
    "name": "germey"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "18",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
    "X-Amzn-Trace-Id": "Root=1-5e5a6344-f03769b7e0c3043b3f759a1a"
  },
  "json": null,
  "origin": "183.230.181.118",
  "url": "https://httpbin.org/post"
}

?

获取cookie

import requests

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
response = requests.get("http://www.baidu.com", headers=headers)
print(response.cookies)
for key, value in response.cookies.items():
    print(key + "=" + value)
<RequestsCookieJar[<Cookie H_PS_PSSID=1444_21125_30839_30823_26350_30717 for .baidu.com/>, <Cookie BDSVRTM=0 for www.baidu.com/>, <Cookie BD_HOME=1 for www.baidu.com/>]>
H_PS_PSSID=1444_21125_30839_30823_26350_30717
BDSVRTM=0
BD_HOME=1

会话维持

模拟登录

import requests

requests.get("http://httpbin.org/cookies/set/number/12345678")
response = requests.get("http://httpbin.org/cookies")
print(response.text)
{
  "cookies": {}
}

?

这相当于用不同的浏览器请求,无法获取和维持会话

import requests

s = requests.Session()
s.get("http://httpbin.org/cookies/set/number/12345678")
response = s.get("http://httpbin.org/cookies")
print(response.text)
{
  "cookies": {
    "number": "12345678"
  }
}

?

证书验证

import requests
from requests.packages import urllib3
urllib3.disable_warnings()
requests.get("https://www.bilibili.com", verify=False)
<Response [200]>

代理设置

import requests
proxies = {
    "http":"http://xxx.xxx:port"
    "https":"https://xxx.xx:port"
    "https":"https://user:[email protected]:port"
}
response = requests.get("https://xxxxxxx", proxies = proxies)

超时设置

import requests
requests.get("http://httpbin.org/get", timeout=1)
<Response [200]>

登录认证设置

import requests
from requests.auth import HTTPBasicAuth
requests.get("url", auth=('用户名', '密码'))

原文地址:https://www.cnblogs.com/codemeta-2020/p/12388376.html

时间: 2024-07-31 06:16:38

python请求库的相关文章

大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url, **kwargs)-->session.request(method="get", url=url, **kwargs)-->session.send(request, **kwargs)-->adapter.send(request, **kwargs)-->

小白学 Python 爬虫(32):异步请求库 AIOHTTP 基础入门

人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Linux基础入门 小白学 Python 爬虫(4):前置准备(三)Docker基础入门 小白学 Python 爬虫(5):前置准备(四)数据库基础 小白学 Python 爬虫(6):前置准备(五)爬虫框架的安装 小白学 Python 爬虫(7):HTTP 基础 小白学 Python 爬虫(8):网页基

python标准库Beautiful Soup与MongoDb爬喜马拉雅电台的总结

Beautiful Soup标准库是一个可以从HTML/XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式,Beautiful Soup将会节省数小时的工作时间.pymongo标准库是MongoDb NoSql数据库与python语言之间的桥梁,通过pymongo将数据保存到MongoDb中.结合使用这两者来爬去喜马拉雅电台的数据... Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是

Python Requests库:HTTP for Humans

Python标准库中用来处理HTTP的模块是urllib2,不过其中的API太零碎了,requests是更简单更人性化的第三方库. 用pip下载: pip install requests 或者git: git clone git://github.com/kennethreitz/requests.git 发送请求: GET方法 >>> import requests >>> r = requests.get('https://api.github.com/event

python标准库_csv

python标准库_csv 简介 所谓的csv(逗号分隔值Comma Separated Values)格式是最通用的用于电子表格和数据库的导入和导出格式.因为没有"csv标准",所以格式被读写它的许多应用程序自由定义.缺乏标准也意味着不同应用程序在产生和使用数据时总是存在一些微小的差异.这些差异使得处理来自多种源的CSV文件时令人头疼.同时,分隔符和引用符的多样性,使得所有格式足够相近以至于编写一个能够有效操作这种数据,对程序员隐藏读写数据细节的独立模块成为可能. 函数 读文件 可以

python Pycurl 库 —— 实现对网站抓包分析

经常使用基调网络的同学,可能对基调网络对页面元素的性能展示感觉很好.它可以做到对一条URL做详细的检测,包括:阻塞时间.DNS解析时间.建立连接时间.SSL握手时间.发出请求时间.首包时间等. 其实,我们也可以做到.比如Python pycurl 库就可以做到对数据的收集,然后可以对收集的数据写入redis或者Mysql.最后前端使用echars通过图形的形式进行展示出来. echars是百度一个开源项目,功能很强大(项目URL:http://echarts.baidu.com)可以将数据通过各

Python 爬虫库

0x00 网络 1)通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络库(绑定libcurl). urllib3 – Python HTTP库,安全连接池.支持文件post.可用性高. httplib2 – 网络库. RoboBrowser – 一个简单的.极具Python风格的Python库,无需独立的浏览器即可浏览网页. MechanicalSoup -一个与网站自动交互Python库. mecha

Python 标准库 BaseHTTPServer 中文翻译

Python 标准库 BaseHTTPServer 中文翻译. 注意: BaseHTTPServer模块在Python3中已被合并到http.server,当转换你的资源为 Python3 时 2to3 工具将自己主动适配导入. 源代码:Lib/BaseHTTPServer.py 此模块定义了两个类用于实现HTTP服务器(Web servers).通常,此模块不被直接使用.可是它用来作为基类创建功能性的Web servers. 查看 SimpleHTTPServer 和 CGIHTTPServe

python标准库之字符编码详解

codesc官方地址:https://docs.python.org/2/library/codecs.html 相关帮助:http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html #python标准库(英文地址:)http://www.ask3.cn/ebook/docspy3zh/library/index.html unicode入门: cpython2.xz支持2种类型字符串处理文本数据,老式的str实例使用单个8位字节表示字