Python爬虫技术 第14节 HTML结构解析

HTML 结构解析是 Web 爬虫中的核心技能之一,它允许你从网页中提取所需的信息。Python 提供了几种流行的库来帮助进行 HTML 解析,其中最常用的是 BeautifulSouplxml

在这里插入图片描述

1. 安装必要的库

首先,你需要安装 requests(用于发送 HTTP 请求)和 beautifulsoup4(用于解析 HTML)。可以通过 pip 安装:

pip install requests beautifulsoup4

2. 发送 HTTP 请求并获取 HTML 内容

使用 requests 库可以轻松地从网站抓取 HTML 页面:

import requestsurl = "https://www.example.com"
response = requests.get(url)# 检查请求是否成功
if response.status_code == 200:html_content = response.text
else:print(f"Failed to retrieve page, status code: {response.status_code}")

3. 解析 HTML 内容

接下来,使用 BeautifulSoup 解析 HTML 内容:

from bs4 import BeautifulSoupsoup = BeautifulSoup(html_content, 'html.parser')

这里的 'html.parser' 是解析器的名字,BeautifulSoup 支持多种解析器,包括 Python 自带的标准库、lxmlhtml5lib

4. 选择和提取信息

一旦你有了 BeautifulSoup 对象,你可以开始提取信息。以下是几种常见的选择器方法:

  • 通过标签名

    titles = soup.find_all('h1')
    
  • 通过类名

    articles = soup.find_all('div', class_='article')
    
  • 通过 ID

    main_content = soup.find(id='main-content')
    
  • 通过属性

    links = soup.find_all('a', href=True)
    
  • 组合选择器

    article_titles = soup.select('div.article h2.title')
    

5. 遍历和处理数据

提取到数据后,你可以遍历并处理它们:

for title in soup.find_all('h2'):print(title.text.strip())

6. 递归解析

对于复杂的嵌套结构,你可以使用递归函数来解析:

def parse_section(section):title = section.find('h2')if title:print(title.text.strip())sub_sections = section.find_all('section')for sub_section in sub_sections:parse_section(sub_section)sections = soup.find_all('section')
for section in sections:parse_section(section)

7. 实战示例

让我们创建一个完整的示例,抓取并解析一个简单的网页:

import requests
from bs4 import BeautifulSoupurl = "https://www.example.com"# 发送请求并解析 HTML
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')# 找到所有的文章标题
article_titles = soup.find_all('h2', class_='article-title')# 输出所有文章标题
for title in article_titles:print(title.text.strip())

这个示例展示了如何从网页中抓取所有具有 class="article-title"h2 元素,并打印出它们的文本内容。

以上就是使用 Python 和 BeautifulSoup 进行 HTML 结构解析的基本流程。当然,实际应用中你可能需要处理更复杂的逻辑,比如处理 JavaScript 渲染的内容或者分页等。

在我们已经讨论的基础上,让我们进一步扩展代码,以便处理更复杂的场景,比如分页、错误处理、日志记录以及数据持久化。我们将继续使用 requestsBeautifulSoup,并引入 loggingsqlite3 来记录日志和存储数据。

1. 异常处理和日志记录

在爬取过程中,可能会遇到各种问题,如网络错误、服务器错误或解析错误。使用 try...except 块和 logging 模块可以帮助我们更好地处理这些问题:

import logging
import requests
from bs4 import BeautifulSouplogging.basicConfig(filename='crawler.log', level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')def fetch_data(url):try:response = requests.get(url)response.raise_for_status()  # Raises an HTTPError for bad responsessoup = BeautifulSoup(response.text, 'html.parser')return soupexcept requests.exceptions.RequestException as e:logging.error(f"Failed to fetch {url}: {e}")return None# Example usage
url = 'https://www.example.com'
soup = fetch_data(url)
if soup:# Proceed with parsing...
else:logging.info("No data fetched, skipping...")

2. 分页处理

许多网站使用分页显示大量数据。你可以通过检查页面源码找到分页链接的模式,并编写代码来遍历所有页面:

def fetch_pages(base_url, page_suffix='page/'):current_page = 1while True:url = f"{base_url}{page_suffix}{current_page}"soup = fetch_data(url)if not soup:break# Process page data here...# Check for next page linknext_page_link = soup.find('a', text='Next')if not next_page_link:breakcurrent_page += 1

3. 数据持久化:SQLite

使用数据库存储爬取的数据可以方便后续分析和检索。SQLite 是一个轻量级的数据库,非常适合小型项目:

import sqlite3def init_db():conn = sqlite3.connect('data.db')cursor = conn.cursor()cursor.execute('''CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT NOT NULL,author TEXT,published_date DATE)''')conn.commit()return conndef save_article(conn, title, author, published_date):cursor = conn.cursor()cursor.execute('''INSERT INTO articles (title, author, published_date) VALUES (?, ?, ?)''', (title, author, published_date))conn.commit()# Initialize database
conn = init_db()# Save data
save_article(conn, "Example Title", "Author Name", "2024-07-24")

4. 完整示例:抓取分页数据并保存到 SQLite

让我们将上述概念整合成一个完整的示例,抓取分页数据并将其保存到 SQLite 数据库:

import logging
import requests
from bs4 import BeautifulSoup
import sqlite3logging.basicConfig(filename='crawler.log', level=logging.INFO)def fetch_data(url):try:response = requests.get(url)response.raise_for_status()return BeautifulSoup(response.text, 'html.parser')except requests.exceptions.RequestException as e:logging.error(f"Failed to fetch {url}: {e}")return Nonedef fetch_pages(base_url, page_suffix='page/'):conn = sqlite3.connect('data.db')cursor = conn.cursor()cursor.execute('''CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT NOT NULL,author TEXT,published_date DATE)''')conn.commit()current_page = 1while True:url = f"{base_url}{page_suffix}{current_page}"soup = fetch_data(url)if not soup:break# Assume the structure of the site allows us to find titles easilytitles = soup.find_all('h2', class_='article-title')for title in titles:save_article(conn, title.text.strip(), None, None)next_page_link = soup.find('a', text='Next')if not next_page_link:breakcurrent_page += 1conn.close()def save_article(conn, title, author, published_date):cursor = conn.cursor()cursor.execute('''INSERT INTO articles (title, author, published_date) VALUES (?, ?, ?)''', (title, author, published_date))conn.commit()# Example usage
base_url = 'https://www.example.com/articles/'
fetch_pages(base_url)

这个示例将抓取 https://www.example.com/articles/ 上的分页数据,保存文章标题到 SQLite 数据库。注意,你需要根据实际网站的 HTML 结构调整 find_allfind 方法的参数。

既然我们已经有了一个基本的框架来抓取分页数据并存储到 SQLite 数据库中,现在让我们进一步完善这个代码,包括添加更详细的错误处理、日志记录、以及处理动态加载的网页内容(通常由 JavaScript 渲染)。

1. 更详细的错误处理

fetch_data 函数中,除了处理请求错误之外,我们还可以捕获和记录其他可能发生的错误,比如解析 HTML 的错误:

def fetch_data(url):try:response = requests.get(url)response.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')return soupexcept requests.exceptions.RequestException as e:logging.error(f"Request error fetching {url}: {e}")except Exception as e:logging.error(f"An unexpected error occurred: {e}")return None

2. 更详细的日志记录

在日志记录方面,我们可以增加更多的信息,比如请求的 HTTP 状态码、响应时间等:

import timedef fetch_data(url):try:start_time = time.time()response = requests.get(url)elapsed_time = time.time() - start_timeresponse.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')logging.info(f"Fetched {url} successfully in {elapsed_time:.2f} seconds, status code: {response.status_code}")return soupexcept requests.exceptions.RequestException as e:logging.error(f"Request error fetching {url}: {e}")except Exception as e:logging.error(f"An unexpected error occurred: {e}")return None

3. 处理动态加载的内容

当网站使用 JavaScript 动态加载内容时,普通的 HTTP 请求无法获取完整的内容。这时可以使用 SeleniumPyppeteer 等库来模拟浏览器行为。这里以 Selenium 为例:

from selenium import webdriver
from selenium.webdriver.chrome.options import Optionsdef fetch_data_with_js(url):options = Options()options.headless = True  # Run Chrome in headless modedriver = webdriver.Chrome(options=options)driver.get(url)# Add wait time or wait for certain elements to loadtime.sleep(3)  # Wait for dynamic content to loadhtml = driver.page_sourcedriver.quit()return BeautifulSoup(html, 'html.parser')

要使用这段代码,你需要先下载 ChromeDriver 并确保它在系统路径中可执行。此外,你还需要安装 selenium 库:

pip install selenium

4. 整合所有改进点

现在,我们可以将上述所有改进点整合到我们的分页数据抓取脚本中:

import logging
import time
import requests
from bs4 import BeautifulSoup
import sqlite3
from selenium import webdriver
from selenium.webdriver.chrome.options import Optionslogging.basicConfig(filename='crawler.log', level=logging.INFO)def fetch_data(url):try:start_time = time.time()response = requests.get(url)elapsed_time = time.time() - start_timeresponse.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')logging.info(f"Fetched {url} successfully in {elapsed_time:.2f} seconds, status code: {response.status_code}")return soupexcept requests.exceptions.RequestException as e:logging.error(f"Request error fetching {url}: {e}")except Exception as e:logging.error(f"An unexpected error occurred: {e}")return Nonedef fetch_data_with_js(url):options = Options()options.headless = Truedriver = webdriver.Chrome(options=options)driver.get(url)time.sleep(3)html = driver.page_sourcedriver.quit()return BeautifulSoup(html, 'html.parser')def fetch_pages(base_url, page_suffix='page/', use_js=False):conn = sqlite3.connect('data.db')cursor = conn.cursor()cursor.execute('''CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT NOT NULL,author TEXT,published_date DATE)''')conn.commit()current_page = 1fetch_function = fetch_data_with_js if use_js else fetch_datawhile True:url = f"{base_url}{page_suffix}{current_page}"soup = fetch_function(url)if not soup:breaktitles = soup.find_all('h2', class_='article-title')for title in titles:save_article(conn, title.text.strip(), None, None)next_page_link = soup.find('a', text='Next')if not next_page_link:breakcurrent_page += 1conn.close()def save_article(conn, title, author, published_date):cursor = conn.cursor()cursor.execute('''INSERT INTO articles (title, author, published_date) VALUES (?, ?, ?)''', (title, author, published_date))conn.commit()# Example usage
base_url = 'https://www.example.com/articles/'
use_js = True  # Set to True if the site uses JS for loading content
fetch_pages(base_url, use_js=use_js)

这个改进版的脚本包含了错误处理、详细的日志记录、以及处理动态加载内容的能力,使得它更加健壮和实用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1488753.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Linux系统上安装zookeeper

百度网盘 通过网盘分享的文件:zookeeper_linux 链接: https://pan.baidu.com/s/1_hybXZVwTRkotz0VbwbSMw?pwd8888 提取码: 8888 1.将压缩包拖进虚拟机 2.解压压缩包 cd /ruanjian/zookeeper/ tar -zxvf apache-ZooKeeper-3.7.2-bin.tar.gz3. 进入到conf目录 cd …

Godot入门 03世界构建1.0版

在game场景,删除StaticBody2D节点,添加TileMap节点 添加TileSet图块集 添加TileSet源 拖动图片到图块,自动创建图块 使用橡皮擦擦除。取消橡皮擦后按住Shift创建大型图块。 进入选择模式,TileMap选择绘制,选中图块后在…

通过强大的语义层增强现代数据湖

在现代数据湖架构中,语义层通过向数据添加有意义的上下文来发挥至关重要的作用,否则这些上下文会丢失。此层充当现代数据湖(数据仓库和查询引擎)处理层中未整理的原始数据与利用此数据的工具和应用程序之间的桥梁。此服务对 AI 特…

【Golang 面试基础题】每日 5 题(七)

✍个人博客:Pandaconda-CSDN博客 📣专栏地址:http://t.csdnimg.cn/UWz06 📚专栏简介:在这个专栏中,我将会分享 Golang 面试中常见的面试题给大家~ ❤️如果有收获的话,欢迎点赞👍收藏…

【中项】系统集成项目管理工程师-第5章 软件工程-5.6软件质量管理与5.7软件过程能力成熟度

前言:系统集成项目管理工程师专业,现分享一些教材知识点。觉得文章还不错的喜欢点赞收藏的同时帮忙点点关注。 软考同样是国家人社部和工信部组织的国家级考试,全称为“全国计算机与软件专业技术资格(水平)考试”&…

ROS机械臂——rviz+gazebo仿真环境搭建

纲要 ROS的控制插件 常用控制器 完善机器人模型 为link添加惯性参数和碰撞属性 为joint添加传动装置 添加gazebo控制插件 加载机器人模型 启动仿真环境 问题:gazebo加载的模型只有一层阴影 解决方案:关闭虚拟机,设置虚拟机属性,…

【数据结构-前缀和】力扣2550.统计范围内的元音字符串数

给你一个下标从 0 开始的字符串数组 words 以及一个二维整数数组 queries 。 每个查询 queries[i] [li, ri] 会要求我们统计在 words 中下标在 li 到 ri 范围内(包含 这两个值)并且以元音开头和结尾的字符串的数目。 返回一个整数数组,其中…

springboot实战(十二)之通过注解的方式记录接口出入参log入库

前言 生产过程中,为了更好的辅助线上问题排查避免不了对接口出入参进行日志输出的时候,并且为了分析接口数据效果需要将每次请求接口的出入参进行落库方便后续的数据分析,这时总不能每个接口入参之后、出参之前都打印一遍日志吧?如…

C++第十弹 ---- vector的介绍及使用

目录 前言vector的介绍及使用1. vector的使用1.1 vector的定义1.2 iterator的使用1.3 vector空间增长问题1.4 vector增删查改 2. vector迭代器失效问题(重点) 总结 前言 本文介绍了C中的vector数据结构及其使用方法。 更多好文, 持续关注 ~ 酷酷学!!! 正文开始 vector的介绍…

基本类型的包装类,面向对象三大特性,继承(inherit).一道力扣分享。

>>>基本类型的包装类 拆包–>封包 拆包–>包装类型转换为基本数据类型 封包—>基本数据类型转换为包装类型 编号基本数据类型包装类型1byteByte2shortShort3charCharacter4intInteger5longLong6floatFloat7doubleDouble8booleanBoolean 为何要用包装类型…

【echarts】中如何设置曲线展示最新值、最大值、最小值

需要用到的属性:图表标注 series-line. markPoint 默认可以通过 type直接标注:‘min’ 最小值、‘max’ 最大值、‘average’ 平均值。 markPoint: {data: [{type: max},{type: min}]}如何展示最新值 如果要展示最新值得话,需要设置 标注…

昇思25天学习打卡营第19天|DCGAN生成漫画头像

DCGAN生成漫画头像总结 实验概述 本实验旨在利用深度卷积生成对抗网络(DCGAN)生成动漫头像,通过设置网络、优化器以及损失函数,使用MindSpore进行实现。 实验目的 学习和掌握DCGAN的基本原理和应用。熟悉使用MindSpore进行图像…

Vue3时间选择器datetimerange在数据库存开始时间和结束时间

♥️作者:小宋1021 🤵‍♂️个人主页:小宋1021主页 ♥️坚持分析平时学习到的项目以及学习到的软件开发知识,和大家一起努力呀!!! 🎈🎈加油! 加油&#xff01…

[算法]归并排序(C语言实现)

一、归并排序的定义 归并排序(Merge sort)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。 二、归并排序的算法原理 归并排序的算法可以用递归法和非递归法来实现…

介绍一下TCP/IP 模型和 OSI 模型的区别

OSI 模型是由国际标准化组织制定的一个用于计算机或通信系统间互联的标准体系,一共有七层,由上而下分别为应用层,表示层,会话层,传输层,网络层,数据链路层和物理层,虽然 OSI 模型理论…

华为网络模拟器eNSP安装部署教程

eNSP是图形化网络仿真平台,该平台通过对真实网络设备的仿真模拟,帮助广大ICT从业者和客户快速熟悉华为数通系列产品,了解并掌握相关产品的操作和配置、提升对企业ICT网络的规划、建设、运维能力,从而帮助企业构建更高效&#xff0…

Geoscene Pro的数据管理

GeoScene Pro是为新一代WebGIS平台而全新打造的一款具有高效、强大生产力且为全面国产的的高级桌面应用程序,可以对来自本地、GeoScene Online、或者GeoScene Portal的数据进行可视化、编辑、分析,可以同时在2D和3D中制作内容,并发布为要素服…

医疗器械维修行业发展及趋势

医疗器械维修的前景是广阔的。‌ 随着医疗技术的不断发展和进步,‌医疗器械的种类和数量持续增加,‌对专业维修人员的需求也在不断上升。‌无论是医院、‌诊所等医疗机构,‌还是医疗器械生产企业、‌销售企业等,‌都需要专业的维修…

Spark+实例解读

第一部分 Spark入门 学习教程:Spark 教程 | Spark 教程 Spark 集成了许多大数据工具,例如 Spark 可以处理任何 Hadoop 数据源,也能在 Hadoop 集群上执行。大数据业内有个共识认为,Spark 只是Hadoop MapReduce 的扩展&#xff08…

C语言常见字符函数和字符串函数精讲

目录 引言 一、字符函数 1.字符分类函数 2.字符转换函数 二、字符串函数 1.gets、puts 2.strlen 3.strcpy 4.strncpy 5.strcat 6.strncat 7.strcmp 8.strncmp 9.strstr 10.strchr 11.strtok 12.strlwr 13.strupr 引言 在C语言编程中,字符函数…