Cant Get The Tag 'rel' Via Beautifulsoup Webscraping Python
I am trying to test a beautifulsoup4 webscrape code on a website. Have done most of it but one attribute information due to its location is little tricky for me to accomplish. Code
Solution 1:
find
returns the element not a list, if you want all a
tags, use the find_all
method. Also to get the rel
attribute you need to use the .get()
method or dictionary lookup. You can also add rel=True
to get only those "a" tags where with the "rel" attribute.
Demo:
Using
find()
>>> soup.find('a', {'id': 'phone-lead', 'rel': True}).get('rel') ['0501365082']
Using
find_all
:>>>for a in soup.find_all('a', {'id':'phone-lead', 'rel': True}):...print(a['rel'])... ['0501365082']
To get a list of all "rel" you can use a list comprehensions
>>> [rel for rel in a['rel'] for a in soup.find_all('a', {'id':'phone-lead', 'rel': True})]
['0501365082']
Post a Comment for "Cant Get The Tag 'rel' Via Beautifulsoup Webscraping Python"