从 HTML 和 XML 文件中提取内容:XPath、字符串、正则表达式示例

本指南将演示如何使用 XPath、字符串匹配和正则表达式从 HTML 和 XML 文件中提取内容。我们将提供详细的代码示例和解释,帮助您理解这些技术并将其应用到您的项目中。

XPath 示例

假设您有一个以下 XML 文件:

<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J.K. Rowling</author>
    <year>2003</year>
    <price>29.99</price>
  </book>
</bookstore>

使用 XPath 表达式提取内容示例:

from lxml import etree

读取 XML 文件

tree = etree.parse("bookstore.xml")

使用 XPath 表达式提取内容

titles = tree.xpath("//book/title/text()") authors = tree.xpath("//book/author/text()") years = tree.xpath("//book/year/text()") prices = tree.xpath("//book/price/text()")

打印提取到的内容

for i in range(len(titles)): print("Title:", titles[i]) print("Author:", authors[i]) print("Year:", years[i]) print("Price:", prices[i]) print("--------------------")

字符串匹配示例

假设您有一个包含以下字符串的变量:

text = "Hello, my name is John. My email is john@example.com."

使用正则表达式提取邮箱示例:

import re

定义字符串

text = "Hello, my name is John. My email is john@example.com."

使用正则表达式提取邮箱

pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}\b" emails = re.findall(pattern, text)

打印提取到的邮箱

for email in emails: print("Email:", email)

正则表达式匹配示例

假设您有一个包含以下字符串的变量:

text = "Hello, my name is John. My email is john@example.com."

使用正则表达式匹配内容示例:

import re

定义字符串

text = "Hello, my name is John. My email is john@example.com."

使用正则表达式匹配内容

pattern = r"name is (\w+)" matches = re.findall(pattern, text)

打印匹配到的内容

for match in matches: print("Match:", match)

以上示例分别演示了在 XML 文件中使用 XPath 提取内容,在字符串中使用正则表达式提取邮箱,以及使用正则表达式匹配内容。</p

从 HTML、XML 文件中提取内容:XPath、字符串、正则表达式示例

原文地址: https://www.cveoy.top/t/topic/p1Pf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录