Using Select isn't finding anything

35 views
Skip to first unread message

Jonathan Moules

unread,
Sep 18, 2015, 7:39:24 AM9/18/15
to beauti...@googlegroups.com
Hi list,
I'm trying to test for the existence of a href tag in some HTML. Looking
at the docs, this should be nice and easy:
http://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors

I have the following code:
print(item)
print(type(item))
print(item.select_one('a'))

which outputs:
<a
href="http://www.example.com/display/87/Specialist.html?Id=1442798&amp;page=1"><span
class="strong">Specialist</span></a>

<class 'bs4.element.Tag'>

None

As you can see, the variable is a bs4.element.Tag, and it definitely has
a value of "href", but it's not being found. In fact, no matter what I
enter into either the select_one or select methods, the output is always
None or [] respectively. I've tried: 'span' and 'a' too, but to no avail.

Any suggestions? Am I doing something wrong?

Python 3.3, with BS 4.

Thanks

leonardr

unread,
Sep 29, 2015, 9:52:20 PM9/29/15
to beautifulsoup, jonatha...@lightpear.com
Jonathan,

When you call select() or select_one() on a tag, it searches the children of that tag. The tag itself is not checked, even if it would match the selector. When you call item.select_one('a') on an <a> tag you're looking for another <a> tag inside the main <a> tag, which doesn't exist.

Leonard
Reply all
Reply to author
Forward
0 new messages