使用美丽的汤在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'}更多推荐
发布评论