当前位置:网站首页>解析网页的完整回顾
解析网页的完整回顾
2022-07-26 22:37:00 【久久鸭要变大鹅子】
文章目录
过程
get(‘URL’)
开发者模式找elements
解析后,整个HTML文档转换成了BeautifulSoup对象
在BeautifulSoup对象中定位目标数据
用BS对象.find_all()找元素对应的节点
点击Elements栏,然后打开搜索框,输入节点名并回车。
打开搜索框方式:Ctrl+F(windows)/Cmd+F(mac) 。
搜索框右侧显示了搜索到的结果数量。
找父节点,在父节点中用Tag对象.find_all()搜索内部节点
属性,HTML元素的 class 属性作为参数名要写成class_。同时使用HTML元素名和HTML元素属性作为搜索条件时,要把HTML元素名作为第 1 个参数。
info_list = info_tag.find_all(‘dl’)
import requests
from bs4 import BeautifulSoup
# 获取网页
# 《乌合之众》网页的 URL
url = 'https://wp.forchange.cn/psychology/11069/'
# 请求网页
res = requests.get(url)
# 打印响应的状态码
print(res.status_code)
# 将响应内容的编码格式设置为utf-8
res.encoding = 'utf-8'
# 解析网页
# 解析请求到的网页,得到 BeautifulSoup 对象
bs = BeautifulSoup(res.text, 'html.parser')
# 用 find() 搜索书籍信息的父节点<div>
info_tag = bs.find('div',class_='res-attrs')
# 用 find_all() 搜索每条信息的节点<dl>
info_list = info_tag.find_all('dl')
# 用 for 循环遍历搜索结果,在每个<dl>节点中继续提取
for info in info_list:
# 用 find() 提取信息提示所在的<dt>节点
dt = info.find('dt')
print(dt)
# 用 find() 提取书籍信息所在的<dd>节点
dd = info.find('dd')
print(dd)
import requests
from bs4 import BeautifulSoup
# 获取网页
# 《乌合之众》网页的 URL
url = 'https://wp.forchange.cn/psychology/11069/'
# 请求网页
res = requests.get(url)
# 打印响应的状态码
print(res.status_code)
# 将响应内容的编码格式设置为utf-8
res.encoding = 'utf-8'
# 解析网页
# 解析请求到的网页,得到 BeautifulSoup 对象
bs = BeautifulSoup(res.text, 'html.parser')
# 搜索书籍信息的父节点<div>
info_tag = bs.find('div', class_='res-attrs')
# 搜索每条信息的节点<dl>
info_list = info_tag.find_all('dl')
# 创建字典,存储书籍信息
info_dict = {
}
# 遍历搜索结果,提取文本内容,存储到字典中
for info in info_list:
# 提取信息提示项<dt>的元素内容
key = info.find('dt').text[:-2]
# 提取书籍信息<dd>的元素内容
value = info.find('dd').text
# 将信息添加到字典中
info_dict[key] = value
# 打印查看字典中的书籍信息
print(info_dict)
获取网页
获取网页的细节有:
1)用 requests 库的 get() 函数请求网页,参数是网页的 URL,即网址,获取网页的结果是得到 Response 对象。
2)通过 Response 对象的响应状态码可以判断请求是否成功。
3)设置 Response 对象的 encoding 属性可以指定编码格式。
4)通过 Response 对象的 text 属性可以获取字符串形式的 HTML 文档。
解析数据
解析数据的细节有:
1)通过实例化 bs4 库的 BeautifulSoup 类实现,结果是一个 BeautifulSoup 对象。
2)实例化 BeautifulSoup 类时,需要传入两个参数:一个是 HTML 文档字符串;一个是解析器。
3)我们用的解析器是 Python 标准库内置的’html.parser’。
4)BeautifulSoup 对象代表整个 HTML 文档,是一种树状结构,每一个节点都是 Tag 对象。
提取数据
提取数据的细节有:
1)目的是从 BeautifulSoup 对象中找到目标 Tag 对象,并提取对应 HTML 元素中的数据。
2)Tag 对象与 HTML 文档中的元素一一对应。通过搜索 Tag 对象就能找到相应的 HTML 元素。
3)可以用 find_all() 和 find() 方法搜索 Tag 对象。
4)通过Tag 对象.text和Tag 对象[‘属性名’],可以提取对应 HTML 元素的文本内容或属性值。
import requests
from bs4 import BeautifulSoup
# 获取网页
# 《乌合之众》网页的 URL
url = 'https://wp.forchange.cn/psychology/11069/'
# 请求网页
res = requests.get(url)
# 打印响应的状态码
print(res.status_code)
# 将响应内容的编码格式设置为utf-8
res.encoding = 'utf-8'
# 解析网页
# 解析请求到的网页,得到 BeautifulSoup 对象
bs = BeautifulSoup(res.text, 'html.parser')
# 搜索书籍信息的父节点<div>
info_tag = bs.find('div', class_='res-attrs')
# 搜索每条信息的节点<dl>
info_list = info_tag.find_all('dl')
# 创建字典,存储书籍信息
info_dict = {
}
# 遍历搜索结果,提取文本内容,存储到字典中
for info in info_list:
# 提取信息提示项<dt>的元素内容
key = info.find('dt').text[:-2]
# 提取书籍信息<dd>的元素内容
value = info.find('dd').text
# 将信息添加到字典中
info_dict[key] = value
边栏推荐
猜你喜欢

Paging plug-in -- PageHelper

【C语言】数组

07 design of ponding monitoring system based on 51 single chip microcomputer

RecBole使用1

第1章 需求分析与ssm环境准备

ResNet论文解读及代码实现(pytorch)

C and pointer Chapter 18 runtime environment 18.1 judgment of runtime environment

文件上传到服务器

Upload files to the server

Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
随机推荐
Blue Bridge Cup brush question notes (word analysis)
Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
卷积神经网络——LeNet(pytorch实现)
09_ Keyboard events
带你熟悉云网络的“电话簿”:DNS
Modulo (remainder) operation in the range of real numbers: how to find the remainder of negative numbers
Codeforces D. Buying Shovels
Identity server4 authorization successful page Jump encountered an error: exception: correlation failed Solution of unknown location
PTA 7-4 small generation (DFS)
Number that cannot be bought
嵌入式系统移植【8】——设备树和根文件系统移植
Analysis of encoding and decoding of encode() and decode(), common encoding and why encode and decode are needed
Practice of data storage scheme in distributed system
deeplabcut使用1
PTA 7-1 play with binary tree
查看 Anaconda 创建环境的位置
Typesript generic constraint
RecBole使用1
Codeforces C1. Simple Polygon Embedding
[Gorm] model relationship -hasone