[英]BeautifulSoup if not find return 0 instead of None
我有以下 Python 語法,使用 BeautifulSoup 進行 web 抓取。
page = soup.find('span', attrs={'class':'h-text-lg'})
對於一個特定頁面,這不會返回任何內容,因為不存在 class。 我應該如何修改代碼以使其返回[0]
而不是None
而不是將None
作為返回值?
您可以使用 Python 的 boolean 表達式返回最后評估值的事實:
page = soup.find('span', attrs={'class':'h-text-lg'}) or [0]
但為什么? 在這種情況下,擁有None
會好得多。
如果page
是找到的元素或None
,任何依賴page
的代碼都可以簡單地檢查if page
或if not page
。 如果page
是[0]
這將不起作用,因為bool([0])
是True
。
如果您不希望它在 class 不存在的情況下返回None
,如果該值不是真值,您可以簡單地更改它
換句話說:
page = soup.find('span', attrs={'class':'h-text-lg'}) or [0]
使用if
語句:
page = soup.find('span', attrs={'class':'h-text-lg'})
if not page:
page = 0
page = soup.find('span', attrs={'class':'h-text-lg'}) or [0]
def find(self, name=None, attrs={}, recursive=True, text=None,
**kwargs):
"""Look in the children of this PageElement and find the first
PageElement that matches the given criteria.
All find_* methods take a common set of arguments. See the online
documentation for detailed explanations.
:param name: A filter on tag name.
:param attrs: A dictionary of filters on attribute values.
:param recursive: If this is True, find() will perform a
recursive search of this PageElement's children. Otherwise,
only the direct children will be considered.
:param limit: Stop looking after finding this many results.
:kwargs: A dictionary of filters on attribute values.
:return: A PageElement.
:rtype: bs4.element.Tag | bs4.element.NavigableString
"""
r = None
l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
if l:
r = l[0]
return r
這就是 find 方法的定義方式,因此您必須實際顯式處理None
情況。 希望這能回答問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.