K S K S - 4 months ago 27
Python Question

python Getting table values from a website

I am trying to extract a particular table values from a website (http://www.forexfactory.com/calendar.php?day=nov18.2016)
using Python BeautifulSoup

The code so far:

from bs4 import BeautifulSoup
from urllib.request import urlopen

content = urlopen("http://www.forexfactory.com/calendar.php?day=nov18.2016").read()
soup = BeautifulSoup(content, 'html.parser')

tables = soup.findAll("table")
for table in tables:
if table.findParent("table") is None:

I could print all multiple tables and it's contents, but how do I get a specific table named "calendar__table" and the iterate though it to get each row and it's values?


You can pass parameter in searching like so:

tables = soup.findAll("table", {'class':'calendar__table'})

then you can iterate trough tables -> rows -> cells:

for table in tables:
    for row in table.findAll("tr"):
        for cell in row.findAll("td"):
            print(cell.text, end = ' ' )