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月前
|
数据采集 存储 开发者
如何动态调整Python爬虫的Request请求延迟
如何动态调整Python爬虫的Request请求延迟
224 13
|
1月前
|
数据采集 Web App开发 前端开发
处理动态Token:Python爬虫应对AJAX授权请求的策略
处理动态Token:Python爬虫应对AJAX授权请求的策略
|
7月前
|
数据采集 JSON API
深入解析:使用 Python 爬虫获取淘宝店铺所有商品接口
本文介绍如何使用Python结合淘宝开放平台API获取指定店铺所有商品数据。首先需注册淘宝开放平台账号、创建应用并获取API密钥,申请接口权限。接着,通过构建请求、生成签名、调用接口(如`taobao.items.search`和`taobao.item.get`)及处理响应,实现数据抓取。代码示例展示了分页处理和错误处理方法,并强调了调用频率限制、数据安全等注意事项。此技能对开发者和数据分析师极具价值。
|
7月前
|
XML JSON API
淘宝商品详情API的调用流程(python请求示例以及json数据示例返回参考)
JSON数据示例:需要提供一个结构化的示例,展示商品详情可能包含的字段,如商品标题、价格、库存、描述、图片链接、卖家信息等。考虑到稳定性,示例应基于淘宝开放平台的标准响应格式。
|
2月前
|
JSON JavaScript API
Python模拟HTTP请求实现APP自动签到
Python模拟HTTP请求实现APP自动签到
|
4月前
|
XML JSON 安全
分析参数顺序对Python requests库进行POST请求的影响。
最后,尽管理论上参数顺序对POST请求没影响,但编写代码时仍然建议遵循一定的顺序和规范,比如URL总是放在第一位,随后是data或json,最后是headers,这样可以提高代码的可读性和维护性。在处理复杂的请求时,一致的参数顺序有助于调试和团队协作。
139 9
|
6月前
|
数据采集 Web App开发 文字识别
Python爬虫多次请求后被要求验证码的应对策略
Python爬虫多次请求后被要求验证码的应对策略
|
7月前
|
算法 测试技术 C语言
深入理解HTTP/2:nghttp2库源码解析及客户端实现示例
通过解析nghttp2库的源码和实现一个简单的HTTP/2客户端示例,本文详细介绍了HTTP/2的关键特性和nghttp2的核心实现。了解这些内容可以帮助开发者更好地理解HTTP/2协议,提高Web应用的性能和用户体验。对于实际开发中的应用,可以根据需要进一步优化和扩展代码,以满足具体需求。
693 29
|
7月前
|
机器学习/深度学习 数据采集 自然语言处理
基于Python的情感分析与情绪识别技术深度解析
本文探讨了基于Python的情感分析与情绪识别技术,涵盖基础概念、实现方法及工业应用。文中区分了情感分析与情绪识别的核心差异,阐述了从词典法到深度学习的技术演进,并通过具体代码展示了Transformers架构在细粒度情感分析中的应用,以及多模态情绪识别框架的设计。此外,还介绍了电商评论分析系统的构建与优化策略,包括领域自适应训练和集成学习等方法。未来,随着深度学习和多模态数据的发展,该技术将更加智能与精准。
435 1
|
7月前
|
前端开发 数据安全/隐私保护 CDN
二次元聚合短视频解析去水印系统源码
二次元聚合短视频解析去水印系统源码
208 4

推荐镜像

更多
  • DNS