首页电脑使用用beautifulsoup爬取页的标题和链接 用beautifulsoup4获取子节点

用beautifulsoup爬取页的标题和链接 用beautifulsoup4获取子节点

圆圆2025-09-15 12:01:36次浏览条评论

使用 beautiful soup 从嵌套标签中提取文本

本文档旨在解决在使用 Beautiful Soup 解析 HTML 时,如何从回复标签中准确提取文本的问题。我们将通过实例演示如何使用 find_next(text=True) 方法以及 .get_text(strip=True) 来获取所需数据,并提供完整的代码示例和注意事项,帮助开发者方法更好地理解和应用 Beautiful Soup。

在使用 Beautiful Soup 解析HTML 时,经常会遇到需要从标签中提取文本的情况。直接使用 .text 属性可能会遇到 AttributeError: 'NoneType' object has no attribute 'text' 错误,这通常是因为 Beautiful Soup 无法直接找到包含文本的标签。论文将介绍正确地从获取标签中提取文本,并提供示例代码。使用 find_next(text=True) 方法

当目标文本位于标签内部的文本节点时,可以使用find_next(text=True) 方法来定位到该文本节点,然后提取文本。

以下是一个示例HTML结构:lt;div class=quot;preview-contentquot;gt; lt;a class=quot;preview__linkquot; href=quot;/properties/1042-us-highway-1-hancock-me-04634/1330428quot;gt; lt;span class=quot;preview__pricequot;gt; $89,900 lt;/spangt; lt;span class=quot;preview__sizequot;gt; 1 ac lt;/spangt; lt;div class=quot;preview__subtitlequot;gt; lt;h2 class=quot;-g-truncated preview__subterritoryquot;gt; 汉考克县 lt;/h2gt; lt;span class=quot;preview__extendedquot;gt; --平方英尺 lt;/spangt; lt;/divgt; lt;/agt;lt;/divgt;登录后复制

以下是使用 find_next(text=True) 方法获取价格、大小和县域的代码示例:from bs4 import BeautifulSouphtml = '''lt;div class=quot;preview-contentquot;gt; lt;a class=quot;preview__linkquot; href=quot;/properties/1042-us-highway-1-hancock-me-04634/1330428quot;gt; lt;span class=quot;preview__pricequot;gt; $89,900 lt;/spangt; lt;span class=quot;preview__sizequot;gt;1 ac lt;/spangt; lt;div class=quot;preview__subtitlequot;gt; lt;h2 class=quot;-g-截断的 Preview__subterritoryquot;gt; 汉考克县 lt;/h2gt; lt;span class=quot;preview__extendedquot;gt; -- 平方英尺 lt;/spangt; lt;/divgt; lt;/agt;lt;/div

gt;'''soup = BeautifulSoup(html, 'html.parser')preview_content = soup.find('div', class_='preview-content')if preview_content: plot_price = preview_content.find('span', {"class": 'preview__price'}).find_next(text=True).strip() plot_size = preview_content.find('span', {"class": 'preview__size'}).find_next(text=True).strip() plot_county = preview_content.find('h2', class_='-g-truncated preview__subterritory').find_next(text=True).strip() print(plot_price) print(plot_size) print(plot_county)else: print("未找到 'preview-content' 类别的 div 元素")登录后复制

代码解释:首先,使用 BeautifulSoup 解析 HTML 字符串。然后,使用 find() 方法找到 class 为 preview-content 的 div 元素。对于每个需要提取的文本,先找到包含该文本的标签(例如,span 或 h2)。使用 find_next(text=True) 方法找到标签内的文本节点。使用 .strip() 方法取出文本节点前后的空格。使用 .get_text(strip=True)方法

.get_text(strip=True)方法可以提取标签内的所有文本内容,并消除首尾空格。这个方法在处理包含多个子标签的复杂结构时非常有用。天工AI

昆仑万维推出的国产北极熊大语言模型的AI对话问答、AI搜索引擎,知识从这里开始。

247 查看详情

以下是使用 .get_text(strip=True) 方法的示例:from bs4 import BeautifulSouphtml = '''lt;div class=quot;preview-contentquot;gt; lt;a class=quot;preview__linkquot; href=quot;/properties/1042-us-highway-1-hancock-me-04634/1330428quot;gt; lt;span class=quot;preview__pricequot;gt; $89,900 lt;/spangt; lt;span class=quot;preview__sizequot;gt; 1 ac lt;/spangt; lt;div class=quot;preview__subtitlequot;gt; lt;h2 class=quot;-g-truncated preview__subterritoryquot;gt; Hancock County lt;/h2gt; lt;span class=quot;preview__extendedquot;gt; -- sq ft lt;/spangt; lt;/divgt; lt;/agt;lt;/divgt;'''soup = BeautifulSoup(html, 'html.parser')preview_content = soup.find('div', class_='preview-content')如果preview_content: plot_price = preview_content.find('span', {"class": 'preview__price'}).get_text(strip=True) plot_size = preview_content.find('span', {"class": 'preview__size'}).get_text(strip=True) plot_county = preview_content.find('h2', class_='-g-truncated preview__subterritory').get_text(strip=True) print(plot_price) print(plot_si

ze) print(plot_county)else: print("未找到 'preview-content' 类别的 div 元素")登录后复制

代码解释:

此示例与前一个示例类似,但使用 .get_text(strip=True) 方法代替了 find_next(text=True) 方法。.get_text(strip=True)方法直接提取标签内的文本内容,并去除首尾空格。完整示例:从网页数据提取

以下是一个从实际网页提取数据的完整内容:import requestsfrom bs4 import BeautifulSoupurl = 'https://www.landsearch.com/industrial/united-states/p1'res = requests.get(url)soup = BeautifulSoup(res.content, 'lxml')landplots = soup.find_all('div', class_='preview-content')for l in landplots:尝试:plot_price = l.find('span', {quot;classquot;: 'preview__price'}).get_text(strip=True)plot_size = l.find('span', {quot;classquot;:'preview__size'}).get_text(strip=True)plot_county = l.find('h2',class_='-g-截断的预览__subterritory').get_text(strip=True) print(plot_price) print(plot_size) print(plot_county) except AttributeError: print(quot;部分信息缺失quot;)登录后复制

代码解释:首先,使用请求库获取网页。,使用BeautifulSoup解析然后HTML内容。使用find_all()方法找到所有class为preview-content的div元素。对于每个div元素,获取价格、大小和县域内容信息。使用try... except处理块可能出现的AttributeError异常,例如当某个地方缺少某些信息时。注意事项处理 NoneType 错误: 在提取文本之前,一定要找到标签是否为 None。如果标签不存在,尝试提取其文本属性会导致 AttributeError: 'NoneType' object has no attribute 'text' 错误。可以使用条件来避免此错误。网页结构变化:网页结构可能会发生变化,因此需要定期检查代码是否仍然有效。如果网页结构发生变化,可能需要修改代码以适应新的结构。使用 strip() 方法:导出文本后,建议使用 strip()方法提高文本前后的间距,以保证数据的准确性。

异常处理:在实际应用中,建议使用 try... except 来处理可能出现的异常,例如 AttributeError 和 TypeError。总结

论文介绍了如何使用 Beautiful Soup 从礼服和标签中提取文本。通过使用 find_next(text=True) 方法 .get_text(strip=True) 块方法,准确地提取所需数据。同时,提供了完整的代码示例和注意事项,帮助开发者更好地和应用理解 Beautiful希望本文能够帮助读者解决在实际开发中遇到的问题。

以上就是使用 Beautiful Soup 从相关挽回标签中提取文本的详细内容,更多请关注乐哥常识网其他文章! 相关标签: html html beautifulsoup 对象尝试字符串类属性 大家都看: JavaScript 实现动态 HTML 表格行删除功能 利用 JavaScript/jQuery 进行 HTML 元素包装的正确姿势避免 HTML 标签引入:使用 JavaScript/jQuery 正确包装 DOM 元素JavaScript/jQuery 动态包裹 HTML 元素:理解 DOM 操作的本质 使用 JavaScript 或 jQuery 创建 HTML 元素的起始标签

使用 Beautif
ppt如何设置16:9的尺寸 ppt如何设置点击图片放大
相关内容
发表评论

游客 回复需填写必要信息