python中request请求库与BeautifulSoup解析库的用法

简介: python中request请求库与BeautifulSoup解析库的用法

python中request请求库与BeautifulSoup解析库的用法

request

安装

打开cmd窗口,检查python环境,需要python3.7版本及以上

然后输入,下载requests库

pip install requests -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

创建项目

创建python文件,最好不要含有中文字符

测试代码

# 1.导入模块
# 1.导入模块
import requests
# 2. 发送请求,获取响应
response = requests.get("http://www.baidu.com")
print(response)  # 这里打印的结果是响应码
# 3. 获取响应数据
# print(response.encoding) # ISO-8859-1
# response.encoding = 'utf-8' # 设置编码格式
# print(response.text)
# 上面两句话等于下面一句话
print(response.content.decode())

运行结果:

小案例(请求疫情首页)

案例代码:

# 1. 导入模块
import requests
# 2. 发送请求,获取响应
response = requests.get("https://ncov.dxy.cn/ncovh5/view/pneumonia")
# 3. 从响应中获取数据
print(response.content.decode())

运行结果:

BeautifulSoup

简介

Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.

安装

运行下面两行命令,或者pycharm可以自动安装。

pip install bs4

pip install lxml

学习代码

# 1. 导入模块
from bs4 import BeautifulSoup
# 2. 创建BeautifulSoup对象
soup = BeautifulSoup('<html>data</html>', 'lxml')
print(soup)

运行结果

find方法

简介

案例(根据标签名查找)

案例代码:

# 1.导入模块
from bs4 import BeautifulSoup
# 2.准备文本字符串
html = '''
    <title>The Dormouse's story</title>
</head>
<body>
<p class="title">
<b>The Dormouse's story</b>
</p>
<p class="story">Once Upon a time three were three little sister;and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, 
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>and 
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well. 
</p>
<p class="story">...</p>
</body>
</html>
'''
# 3.创建BeautifulSoup对象
soup = BeautifulSoup(html,'lxml')
# 4.查找title标签
title = soup.find('title')
print(title)
# 5.查找a标签
a = soup.find('a')
print(a)
#查找所有a标签
a_s = soup.find_all('a')
print(a_s)

运行结果:

案例(根据属性查找)

案例代码

# 1.导入模块
from bs4 import BeautifulSoup
# 2.准备文本字符串
html = '''
    <title>The Dormouse's story</title>
</head>
<body>
<p class="title">
<b>The Dormouse's story</b>
</p>
<p class="story">Once Upon a time three were three little sister;and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, 
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>and 
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well. 
</p>
<p class="story">...</p>
</body>
</html>
'''
# 3.创建BeautifulSoup对象
soup = BeautifulSoup(html,'lxml')
# 二、根据属性查找
#查找 id 为 link1 的标签
#方法一:通过命名参数进行查找
a = soup.find(id = 'link1')
print(a)
#方法二:使用attrs来指定属性字典,进行查找
a = soup.find(attrs={'id':'link1'})
print(a)

运行结果

案例(根据文本查找)

案例代码

# 1.导入模块
from bs4 import BeautifulSoup
# 2.准备文本字符串
html = '''
    <title>The Dormouse's story</title>
</head>
<body>
<p class="title">
<b>The Dormouse's story</b>
</p>
<p class="story">Once Upon a time three were three little sister;and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, 
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>and 
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well. 
</p>
<p class="story">...</p>
</body>
</html>
'''
# 3.创建BeautifulSoup对象
soup = BeautifulSoup(html,'lxml')
#三、根据文本查找
# 获取下面文档中文本为 Elsie 的标签文本
text = soup.find(text='Elsie')
print(text)

运行结果

案例(Tag属性使用)

案例代码

# 1.导入模块
from bs4 import BeautifulSoup
# 2.准备文本字符串
html = '''
    <title>The Dormouse's story</title>
</head>
<body>
<p class="title">
<b>The Dormouse's story</b>
</p>
<p class="story">Once Upon a time three were three little sister;and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, 
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>and 
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well. 
</p>
<p class="story">...</p>
</body>
</html>
'''
# 3.创建BeautifulSoup对象
soup = BeautifulSoup(html,'lxml')
a = soup.find(attrs={'id':'link1'})
#Tag对象
print(type(a))  #<class 'bs4.element.Tag'>
print('标签名:',a.name)
print('标签所有属性:',a.attrs)  #输出的class是一个列表,class 一个属性中可以有多个值
print('标签文本内容:',a.text)

运行结果

案例(从疫情首页提取各国最新的疫情数据)

ctrl+f查找某个类型元素的区域,然后,需找到对应标签的id,然后根据id的值来通过find方法获取文本内容。

案例代码:

# 1.导入相关模块
import requests
from bs4 import BeautifulSoup
# 2.发送请求,获取疫情首页内容
response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page = response.content.decode()
#print(home_page)
# 3.使用 BeautifulSoup 获取疫情数据
soup = BeautifulSoup(home_page, 'lxml')
script = soup.find(id='getAreaStat')
text = script.text
print(text)

运行结果:

相关文章
|
6月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
774 0
|
6月前
|
数据采集 存储 JavaScript
解析Python爬虫中的Cookies和Session管理
Cookies与Session是Python爬虫中实现状态保持的核心。Cookies由服务器发送、客户端存储,用于标识用户;Session则通过唯一ID在服务端记录会话信息。二者协同实现登录模拟与数据持久化。
|
7月前
|
JSON 缓存 开发者
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
本文详解淘宝开放平台taobao.item_get接口对接全流程,涵盖参数配置、MD5签名生成、Python企业级代码实现及高频问题排查,提供可落地的实战方案,助你高效稳定获取商品数据。
|
7月前
|
存储 大数据 Unix
Python生成器 vs 迭代器:从内存到代码的深度解析
在Python中,处理大数据或无限序列时,迭代器与生成器可避免内存溢出。迭代器通过`__iter__`和`__next__`手动实现,控制灵活;生成器用`yield`自动实现,代码简洁、内存高效。生成器适合大文件读取、惰性计算等场景,是性能优化的关键工具。
375 2
|
7月前
|
机器学习/深度学习 文字识别 Java
Python实现PDF图片OCR识别:从原理到实战的全流程解析
本文详解2025年Python实现扫描PDF文本提取的四大OCR方案(Tesseract、EasyOCR、PaddleOCR、OCRmyPDF),涵盖环境配置、图像预处理、核心识别与性能优化,结合财务票据、古籍数字化等实战场景,助力高效构建自动化文档处理系统。
1796 0
|
7月前
|
机器学习/深度学习 JSON Java
Java调用Python的5种实用方案:从简单到进阶的全场景解析
在机器学习与大数据融合背景下,Java与Python协同开发成为企业常见需求。本文通过真实案例解析5种主流调用方案,涵盖脚本调用到微服务架构,助力开发者根据业务场景选择最优方案,提升开发效率与系统性能。
1677 0
机器学习/深度学习 算法 自动驾驶
1230 0
|
7月前
|
算法 安全 数据安全/隐私保护
Python随机数函数全解析:5个核心工具的实战指南
Python的random模块不仅包含基础的随机数生成函数,还提供了如randint()、choice()、shuffle()和sample()等实用工具,适用于游戏开发、密码学、统计模拟等多个领域。本文深入解析这些函数的用法、底层原理及最佳实践,帮助开发者高效利用随机数,提升代码质量与安全性。
1137 0
|
7月前
|
数据可视化 Linux iOS开发
Python脚本转EXE文件实战指南:从原理到操作全解析
本教程详解如何将Python脚本打包为EXE文件,涵盖PyInstaller、auto-py-to-exe和cx_Freeze三种工具,包含实战案例与常见问题解决方案,助你轻松发布独立运行的Python程序。
1706 2
|
7月前
|
设计模式 缓存 运维
Python装饰器实战场景解析:从原理到应用的10个经典案例
Python装饰器是函数式编程的精华,通过10个实战场景,从日志记录、权限验证到插件系统,全面解析其应用。掌握装饰器,让代码更优雅、灵活,提升开发效率。
521 0

推荐镜像

更多
  • DNS