8๐
โ
You can use the find
method on the soup object and find the tags with specific attributes. Here we need to find the meta
tag with either name
attribute equal to og:description
or description
or property
attribute equal to description
.
# First get the meta description tag
description = soup.find('meta', attrs={'name':'og:description'}) or soup.find('meta', attrs={'property':'description'}) or soup.find('meta', attrs={'name':'description'})
# If description meta tag was found, then get the content attribute and save it to db entry
if description:
entry.description = description.get('content')
๐คvivekagr
2๐
You could do something like this:
# Order these in order of preference
description_selectors = [
{"name": "description"},
{"name": "og:description"},
{"property": "description"}
]
for selector in description_selectors:
description_tag = soup.find(attrs=selector)
if description_tag and description_tag.get('content'):
description = description_tag['content']
break
else:
desciption = ''
Just note, the else is for the for
, and not for the if
.
๐คzsquare
Source:stackexchange.com