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

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介: 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)

运行结果:

相关文章
|
1月前
|
数据采集 XML API
深入解析BeautifulSoup:从sohu.com视频页面提取关键信息的实战技巧
深入解析BeautifulSoup:从sohu.com视频页面提取关键信息的实战技巧
|
2月前
|
数据采集 XML 数据格式
解析Amazon搜索结果页面:使用BeautifulSoup
解析Amazon搜索结果页面:使用BeautifulSoup
|
2月前
|
JSON Shell Linux
dockerfile 用法全解析
Dockerfile指令简介:`FROM`基于Alpine镜像;`WORKDIR`设置工作目录;`COPY`复制文件;`ADD`支持URL;`RUN`运行命令;`CMD`容器启动时执行;`ENTRYPOINT`与`CMD`组合执行;`EXPOSE`声明端口;`VOLUME`映射文件;`ENV`设置环境变量;`ARG`构建参数;`LABEL`元数据;`ONBUILD`触发命令;`STOPSIGNAL`停止信号;`HEALTHCHECK`健康检查;`SHELL`默认Shell。Alpine仅5M,小巧高效。
66 4
dockerfile 用法全解析
|
3月前
|
数据采集 JavaScript API
网页解析库:BeautifulSoup与Cheerio的选择
网页解析库:BeautifulSoup与Cheerio的选择
|
3月前
|
存储 Go PHP
Go语言中的加解密利器:go-crypto库全解析
在软件开发中,数据安全和隐私保护至关重要。`go-crypto` 是一个专为 Golang 设计的加密解密工具库,支持 AES 和 RSA 等加密算法,帮助开发者轻松实现数据的加密和解密,保障数据传输和存储的安全性。本文将详细介绍 `go-crypto` 的安装、特性及应用实例。
202 0
|
3月前
|
Python
Python三引号用法与变量详解
本文详细介绍了Python中三引号(`&quot;&quot;&quot;` 或 `&#39;&#39;&#39;`)的用法,包括其基本功能、如何在多行字符串中使用变量(如f-string、str.format()和%操作符),以及实际应用示例,帮助读者更好地理解和运用这一强大工具。
163 2
|
3月前
|
Dart 安全 编译器
Flutter结合鸿蒙next 中数据类型转换的高级用法:dynamic 类型与其他类型的转换解析
在 Flutter 开发中,`dynamic` 类型提供了灵活性,但也带来了类型安全性问题。本文深入探讨 `dynamic` 类型及其与其他类型的转换,介绍如何使用 `as` 关键字、`is` 操作符和 `whereType&lt;T&gt;()` 方法进行类型转换,并提供最佳实践,包括避免过度使用 `dynamic`、使用 Null Safety 和异常处理,帮助开发者提高代码的可读性和可维护性。
127 1
|
4月前
|
XML 前端开发 数据格式
Beautiful Soup 解析html | python小知识
在数据驱动的时代,网页数据是非常宝贵的资源。很多时候我们需要从网页上提取数据,进行分析和处理。Beautiful Soup 是一个非常流行的 Python 库,可以帮助我们轻松地解析和提取网页中的数据。本文将详细介绍 Beautiful Soup 的基础知识和常用操作,帮助初学者快速入门和精通这一强大的工具。【10月更文挑战第11天】
115 2
|
4月前
|
前端开发 JavaScript UED
axios取消请求CancelToken的原理解析及用法示例
axios取消请求CancelToken的原理解析及用法示例
289 0
|
1月前
|
自然语言处理 数据处理 索引
mindspeed-llm源码解析(一)preprocess_data
mindspeed-llm是昇腾模型套件代码仓,原来叫"modelLink"。这篇文章带大家阅读一下数据处理脚本preprocess_data.py(基于1.0.0分支),数据处理是模型训练的第一步,经常会用到。
53 0