Answer a question

<h2 class="hello-word"><a href="http://www.google.com">Google</a></h2>

How do I grab the value of the a tag (Google)?

print soup.select("h2 > a")

returns the entire a tag and I just want the value. Also, there could be multiple H2s on the page. How do I filter for the one with the class hello-word?

Answers

You can use .hello-word on h2 in the CSS Selector, to select only h2 tags with class hello-word and then select its child a . Also soup.select() returns a list of all possible matches, so you can easily iterate over it and call each elements .text to get the text. Example -

for i in soup.select("h2.hello-word > a"):
    print(i.text)

Example/Demo (I added a few of my own elements , one with a slightly different class to show the working of the selector) -

>>> from bs4 import BeautifulSoup
>>> s = """<h2 class="hello-word"><a href="http://www.google.com">Google</a></h2>
... <h2 class="hello-word"><a href="http://www.google.com">Google12</a></h2>
... <h2 class="hello-word2"><a href="http://www.google.com">Google13</a></h2>"""

>>> soup = BeautifulSoup(s,'html.parser')

>>> for i in soup.select("h2.hello-word > a"):
...     print(i.text)
...
Google
Google12
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐