0
votes

I am learning web scrape using BeautifulSoup.

Trying to extract the different elements from the HTML, but there is a problem when I extract data from meta tag. The list for meta is as below picture: enter image description here

or slist6 pasted as below:

[<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>, <meta content="width=device-width,initial-scale=1" name="viewport"/>, <meta content="no-referrer" name="referrer"/>, <meta content="never" name="referrer"/>, <meta content="VPPXtECgUUeuATBacnqnCm4ydGO99reF-xgNklSbNbc" name="google-site-verification"/>, <meta content="034F16304017CA7DCF45D43850915323" name="msvalidate.01"/>, <meta content="" name="description"/>]

The attribute as content works in the code but doesn't work for name. It shows Keyerror: name, The results of the codes as below picture. May I ask for your help on how to get the name attribute? Thanks.

enter image description here

enter image description here

enter image description here

1
It would be helpful if you paste the result of slist1.find_all('meta') instead of attaching a screenshot of it. This would make testing easierSushil
Thank for your suggestion,Sushil. I pasted the result.Annie

1 Answers

1
votes

For checking whether attribute name is present in element method has_attr() can be used:

for meta in slist6:
    if meta.has_attr('name'):
        print(meta['name'])
    else:
        print('not exist')