使用美丽的汤在python中检索`h1 id`(Retrieving `h1 id` in python using beautiful soup)

编程入门 行业动态 更新时间:2024-10-23 18:33:33
使用美丽的汤在python中检索`h1 id`(Retrieving `h1 id` in python using beautiful soup)

真的很简单。 对于以下html代码:

<h1 id="product-name" itemprop="name">Best product name !</h1>

我想检索最佳产品名称! 并且这样做我目前正在使用:

prodname = soup.find(id="product-name") prodname_clean = list(prodname.children)[0] print(prodname_clean)

但在某些情况下,我收到以下错误:

AttributeError: 'NoneType' object has no attribute 'children'

为什么我在某些情况下会出现此错误,而其他情况则不是有点神秘,但无论如何我检索h1的方式很可能不是最好的。 任何帮助将不胜感激。

Pretty simple really. For the following html code:

<h1 id="product-name" itemprop="name">Best product name !</h1>

I would like to retrieve Best product name ! and to do so I am currently using:

prodname = soup.find(id="product-name") prodname_clean = list(prodname.children)[0] print(prodname_clean)

But on some occasions I get the following error:

AttributeError: 'NoneType' object has no attribute 'children'

Why I get this error on some occasions and not others is a bit of a mystery but in any case my manner of retrieving h1 is most probably not the best one. Any help would be greatly appreciated.

最满意答案

你可以这样做:

>>> soup.find('h1').text 'Best product name !'

或者更确切地说,

>>> soup.find('h1', {'id': 'product-name'}).text 'Best product name !'

您可以在字典中添加更多属性,例如

{'id': 'product-name', 'itemprop': 'name'}

You can simply do this:

>>> soup.find('h1').text 'Best product name !'

Or to be more precise,

>>> soup.find('h1', {'id': 'product-name'}).text 'Best product name !'

You can add more attributes in the dictionary, like

{'id': 'product-name', 'itemprop': 'name'}

更多推荐

本文发布于:2023-07-08 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:美丽   python   id   beautiful   Retrieving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!