python - BS4 + Python3: unable to crawl tree: 'NavigableString' object has no attribute 'has_attr' -



i'm new python (i know powershell) , i'm trying learn web crawling bs4+python3.

here's simple exercise practicing with:

<h1 class="entry-title"> <a href="test1.html">test1</a></h1> <h1 class="entry-title"> <a href="test2.html" rel="bookmark">test2</a></h1> 

what want details (href , .string) attributes "rel"

heres code

for h1_tag in soup.find_all(("h1", { "class" : "entry-title" })):     a_tag in h1_tag.contents:         if a_tag.has_attr('rel'):            print (a_tag) 

but i'm getting: attributeerror: 'navigablestring' object has no attribute 'has_attr'

what doing wrong? appreciated.

thanks!

you iterating on contents, including navigablestring objects; e.g. text.

if wanted find elements rel attribute, search them instead:

for h1_tag in soup.find_all(("h1", { "class" : "entry-title" })):     a_tag in h1_tag.find_all('a', rel=true):        print(a_tag) 

the rel=true keyword argument constrains search elements have attribute; <a> tags no rel attribute skipped.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -