Python爬虫—requests模块简单应用

Python爬虫---requests模块简介

requests的作用与安装

作用:发送网络请求,返回响应数据

安装:pip install requests

requests模块发送简单的get请求、获取响应

需求:通过requests向百度首页发送请求,获取百度首页的数据

python 复制代码
import requests

# 目标url
url = 'https://www.baidu.com'

# 向目标url发送get请求
response = requests.get(url)

# 打印响应内容
print(response.text)

# 获取响应
res = requests.get(url, headers=headers)
print(res)
print(type(res))

# 获取请求的url, 响应的编码方式
print(res.url)
res.encoding = 'utf-8'

# 获取响应内容,会使用默认编码(有时候会乱码, 需要指定编码)
print(res.encoding)
print(res.text)
print(res.content.decode('utf-8'))

# 获取请求状态码
print(res.status_code)

# 获取响应对应的请求头
print(res.request.headers)

# 获取相应的cookie
print(res.cookies)
response的常用属性:
  • response.text 响应体str类型
  • response.encoding 从HTTP header中猜测的响应内容的编码方式
  • response.content 响应体bytes类型
  • response.status_code 响应状态码
  • response.requests.headers 响应对应的请求头
  • response.headers 响应头
  • response.cookies 响应的cookie(经过了set-cookie动作)
  • response.url 获取访问的url
  • response.json() 获取json数据得到内容为字典(如果接口响应体的格式是json格式时)
  • response.ok

​ 如果status_code小于200,response.ok返回True。

​ 如果status_code大于200,response.ok返回False。

response.text和response.content的区别

  • response.text
    • 类型:str
    • 解码类型:requests模块自动根据HTTP头部对响应的编码作出有根据的推测,推测的文本编码
    • 如何修改编码方式:response.encoding="gbk/UTF-8"
  • response.content
    • 类型:bytes
    • 解码类型:没有指定
    • 如何修改编码方式:response.content.decode('utf-8')

获取网页源码的通用方式:

python 复制代码
response.content.decode()
response.content.decode('utf-8')
response.text

requests下载图片

python 复制代码
# 在百度首页搜索李小龙图片,然后下载到本地
import requests

url = 'https://photocdn.sohu.com/20080725/Img258368622.jpg'
res = requests.get(url)
with open('李小龙.jpg', 'wb') as f:
    f.write(res.content)
import requests

url = 'https://photocdn.sohu.com/20080725/Img258368622.jpg'
# 请求
res = requests.get(url)
# 写入
with open('李小龙.jpg', 'wb') as f:
		# 写入文件 注意:必须二进制形式
    f.write(res.content)

发送带header请求头的请求

python 复制代码
import requests

headers = {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}

url = 'https://i3.sinaimg.cn/ent/m/c/2010-11-26/U3987P28T3D3159293F326DT20101126121845.jpg'
# 请求
res = requests.get(url, headers=headers)

# 写入
with open('李小龙1.jpg', 'wb') as f:
  f.write(res.content)

发送带参数的请求

  • GET请求
python 复制代码
import requests

headers = {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
kw = {
  'wd': '李小龙'
}
url = 'https://www.baidu.com/s?'

res = requests.get(url, params=kw, headers=headers)
print(res.content.decode('utf-8'))
  • POST请求
python 复制代码
import requests

headers = {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
url = 'https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_web_new_fanyi&sign=NyHAgRlbDg6%2BBbQIiKcntdRriqVIAJSQ%2BxmfU0q7dIE%3D'
# post 携带表单数据
form_data = {
'from': 'zh',
'to': 'en',
'q': '我爱你中国'
}

# 发送post请求
res = requests.post(url, data=form_data, headers=headers)
print(res.json())
相关推荐
985小水博一枚呀22 分钟前
【深度学习|可视化】如何以图形化的方式展示神经网络的结构、训练过程、模型的中间状态或模型决策的结果??
人工智能·python·深度学习·神经网络·机器学习·计算机视觉·cnn
CyreneSimon1 小时前
使用 LoRA 进行模型微调的步骤
python·transformer
ymchuangke1 小时前
数据清洗-缺失值处理-缺失值可视化图(竖线)
python·算法·数学建模
计算机学姐1 小时前
基于python+django+vue的旅游网站系统
开发语言·vue.js·python·mysql·django·旅游·web3.py
程序员小羊!2 小时前
Python语言基础教程(下)4.0
开发语言·python
huanxiangcoco2 小时前
73. 矩阵置零
python·leetcode·矩阵
一晌小贪欢2 小时前
Python基础知识——字典排序(不断补充)
python·json·python基础·字典·字典排序·python学习
YOLO数据集工作室2 小时前
Python介绍
开发语言·python
Hiweir ·3 小时前
机器翻译之创建Seq2Seq的编码器、解码器
人工智能·pytorch·python·rnn·深度学习·算法·lstm
不染_是非3 小时前
Django学习实战篇六(适合略有基础的新手小白学习)(从0开发项目)
后端·python·学习·django