之前一直想写个爬虫,但都只是零零散散的学也没有做个笔记啥的,导致现在一些知识点都差不多忘光了…(前车之鉴啊学东西一定要写点笔记啊,不然时间一久就又得从头开始学了)

所以现在通过写博客记录学习的过程。

前期准备:
安装好 requests库
pip install requests

参考request用户手册 requests库 用法:

函数

用法

requests.get(url,params=)

get请求,提交的参数在url中,返回页面内容

Response响应中的属性和方法
常用属性:

status_code: 数据类型:int
作用:返回HTTP响应的状态码:200、404、500、等
reason: 数据类型:str
作用:返回HTTP响应的描述:OK、Not Found、等
headers: 数据类型:字典
作用:返回HTTP响应头
url: 数据类型:str
作用:请求的真实地址
history: 数据类型:列表(数组)
作用:访问历史记录(重定向记录)
encoding: 数据类型:str
作用:HTTP响应头中的编码字段,response.text
取反回值时,就是根据这个字段进行解码的,如果没有,则按:"ISO-8859-1"解码
cookies: 数据类型:RequestsCookieJar
作用:获取cookie
elapsed:类型:datetime.timedelta
作用:发送请求到接收到响应所花费的时长
request: 类型:requests.models.PreparedRequest
作用:对应的请求对象
raw:作用:原始响应体,也就是 urllib 的 HTTPResponse 对象,使用 response.raw.read() 读取
加粗样式
常用方法
@property类型方法:
ok(self):状态码是否小于400,返回值为True或False
apparent_encoding(self):根据返回内容, 解析出来的字符编码
content(self):原始响应体
text(self):经过编码后的文本内容

普通方法:

**json(self, kwargs):转换成json格式的数据
iter_content(self, chunk_size=1, decode_unicode=False):按字节分块的迭代器
iter_lines(self,chunk_size=ITER_CHUNK_SIZE,decode_unicode=None,delimiter=None):按行分块的迭代器
raise_for_status(self):抛出状态异常错误
close(self):关闭连接

import requests
    #requests库函数测试
   
url='http://httpbin.org/get'
url1='http://github.com'
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"}
payload={'key1':'value'}
#测试get请求
response=requests.get(url1,headers=headers,params=payload,timeout=10)
print('返回状态码:',response.status_code)
print('根据HTTP头部推测编码:',response.encoding)
print('根据页面内容判断编码:',response.apparent_encoding)
print('以二进制形式返回页面内容:',response.content)
print('JSON形式返回内容:',response.json)
print('返回cookie:',response.cookies)
print('发送请求到响应到达所经过的时间:',response.elapsed)
print('响应头:',response.headers)
print('返回包含请求(URL)历史的响应对象列表:(追踪重定向。)',response.history)
print('原始响应头:',response.raw)
print('返回与状态码相对应的文本:',response.reason)
print('请求对象:',response.request)
print('请求的真实url:',response.url)
print('编码后返回的页面内容',response.text)

res的用法Python python中的res_数据类型


res的用法Python python中的res_python_02


res的用法Python python中的res_python_03

函数

用法

requests.post(url,data=)

post请求,提交的参数在url中,返回页面内容

url='http://httpbin.org/post'
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"}
payload={'key':'balue'}
#payload=(('key1', 'value1'), ('key1', 'value2'))
response=requests.post(url,data=payload,headers=headers,timeout=10)
print('返回状态码:',response.status_code)
print('根据HTTP头部推测编码:',response.encoding)
print('根据页面内容判断编码:',response.apparent_encoding)
print('以二进制形式返回页面内容:',response.content)
print('JSON形式返回内容:',response.json)
print('返回cookie:',response.cookies)
print('发送请求到响应到达所经过的时间:',response.elapsed)
print('响应头:',response.headers)
print('返回包含请求(URL)历史的响应对象列表:(追踪重定向。)',response.history)
print('原始响应头:',response.raw)
print('返回与状态码相对应的文本:',response.reason)
print('请求对象:',response.request)
print('请求的真实url:',response.url)
print('编码后返回的页面内容',response.text)

res的用法Python python中的res_res的用法Python_04

函数

用法

requests.head(url,**kwargs)

head请求,返回响应头

url='http://httpbin.org/head'
response=requests.head(url)
print(response.headers)

res的用法Python python中的res_python_05

函数

解释

requests.put(url,data=,**kwargs=)

#向html网页提交put请求的方法

requests.patch(url,data,**kwargs=)

#向html提交局部修改的请求

requests.delete(url,**kwargs=)

#向html提交删除请求

requests.request(method,url,**kwargs)

method: “GET”、”HEAD”、”POST”、”PUT”、”PATCH”; url: 请求的网址;**kwargs: 控制访问的参数

payload={"key1":"value1","key2":"value2"}
r1=requests.put("http://httpbin.org/put",data=payload)
files={'files':open('C:\\VSCode_work\\python_work\\CTF脚本\\test.txt','rb')}
r2 = requests.post('https://httpbin.org/post',files=files)

print(r1.text)
print(r2.text)

res的用法Python python中的res_res的用法Python_06


res的用法Python python中的res_python_07

r3=requests.request(method='POST',url="http://httpbin.org/post",data=payload)
print(r3.request)
print(r3.text)

res的用法Python python中的res_数据类型_08