Python `beautifulsoup` extraction on urls lacking `class`, other attributes?

Viewed 48

Quick question [I am not very familiar with Python's BeautifulSoup()] If I have the following element,

how can I extract/get "1 comment" (or, "2 comments", etc.)? There is no class (or id, or other attributes) in that "a" tag.

<td class="subtext">
  <a href="item?id=22823679">1&nbsp;comment</a>
</td>
2 Answers

You can use select method to apply a querySelect into your html, and then take the contents of the elements you found:

elements = soup.select(".subtext a")
[x.contents for x in elements]

How about the following, test with local html file

from bs4 import BeautifulSoup

url = "D:\\Temp\\example.html"

with open(url, "r") as page:
    contents = page.read()
    soup = BeautifulSoup(contents, 'html.parser')
    element = soup.select('td.subtext')
    value = element[0].get_text()
    print(value)

example.html

<html>
    <head></head>
        <body>
            <td class="subtext">
                <a href="item?id=22823679">1&nbsp;comment</a>
            </td>
        </body>
</html>
Related