malleYay
malleYay

Reputation: 133

getting data from html table with BeautifulSoup

I have troubles extracting data from a html table. I have the following python code, but it gives me an error message:

File "request_test.py", line 50 print(soup.find_all("td", class="station tqdetail top")) ^ SyntaxError: invalid syntax

from bs4 import BeautifulSoup

html_doc = """
<html>
<body>

<table>
<tr>
<th colspan="2">an</th>
<th>Halt</th>
<tr>
<td class="arrival tqdetail">
17:24
<br />
17:24
</td>
<td class="tqdetail rt top">
<span class="okmsg bold">+0</span>
<br />
<span class="okmsg bold">+0</span>
</td>
<td class="station tqdetail top">
Foo
<br />
</td>
</tr>

</body>
</html>
"""

soup = BeautifulSoup(html_doc)

print(soup.find_all("td", class="station tqdetail top"))

I don't get what I'm doing wrong here, I appreciate any clues here.

Upvotes: 0

Views: 123

Answers (2)

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Here how you can use class in soup:

print(soup.find_all("td", {"class":"station tqdetail top"}))

Upvotes: 0

Aaron Perley
Aaron Perley

Reputation: 629

class is a reserved word in python. The function should be soup.find_all("td", class_="station tqdetail top") (notice the underscore after class). See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class for more info.

Upvotes: 0

Related Questions