Python (九)requests


程序员的公众号:源1024获取更多资料,无加密无套路!

最近整理了一波电子书籍资料,包含《Effective Java中文版 第2版》《深入JAVA虚拟机》,《重构改善既有代码设计》,《MySQL高性能-第3版》,《Java并发编程实战》等等
获取方式: 关注公众号并回复 电子书 领取,更多内容持续奉上


使用 requests 发送 HTTP 请求

安装requests模块:

import requests

发送 HTTP 请求:

import requests


resp = requests.get('http://baidu.com')
#响应状态码
print('status_code: ',resp.status_code)
#响应头
print('headers: ',resp.headers)
#响应内容
print('content: ',resp.content)
#网页内容
print('text: ',resp.text)

print(resp.reason)
print(resp.is_redirect)
print(resp.url)
print(resp.cookies)
print(resp.encoding)

#输出
status_code:  200
headers:  {'Date': 'Fri, 15 Dec 2023 06:46:57 GMT', 'Server': 'Apache', 'Last-Modified':             
         'Tue, 12 Jan 2010 13:48:00 GMT', 'ETag': '"51-47cf7e6ee8400"', 'Accept-Ranges': 
         'bytes', 'Content-Length': '81', 'Cache-Control': 'max-age=86400', 'Expires': 
         'Sat, 16 
          Dec 2023 06:46:57 GMT', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html'}
content:  b'<html>\n<meta http-equiv="refresh" 
         content="0;url=http://www.baidu.com/">\n</html>\n'
text:  <html>
         <meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
       </html>

OK
False
http://baidu.com/
<RequestsCookieJar[]>
ISO-8859-1
属性
content响应的内容,以字节为单位
cookies返回一个 CookieJar 对象,包含了从服务器发回的 cookie
encoding解码方式
headers返回响应头,字典格式
is_redirect如果响应被重定向,则返回 True,否则返回 False
reason响应状态的描述
request返回请求此响应的请求对象
text返回响应的内容
url返回响应的URL
requests方法

delete(url, args)

发送 DELETE 请求到指定 url

get(url, params, args)

发送 GET 请求到指定 url

post(url, data, json, args)

发送 POST 请求到指定 url

put(url, data, args)

发送 PUT 请求到指定 url

request(method, url, args)

发送指定的请求方法

附加参数
headers

设置请求头,如'User-Agent': 'Mozilla/5.0'

params

查询参数,{'key1': 'value1'}

data

请求体,{'username': '用户名'}  


系列文章索引

Python (一) 操作Mysql

Python (二) 读写excel文件

Python (三) 读写csv文件

Python (四)读写word

Python (五) 处理图像

Python (六) 绘图

Python(七)操作JSON


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值