tkpoum
tkpoum

Reputation: 1

BeautifulSoup choose the table

On the website there are few tables. I want to choose one of them and there is my problem. When I write:

g_data=soup.find_all("table",{"class":"awT votegroup votegroup7 wH episodesList"}, {"id":"sezon7"}) 

it finds me exactly only table nr 7, but when I write as following:

html_1=("table",{"class":"awT votegroup votegroup7 wH episodesList"}, {"id":"sezon7"})   
g_data=soup.find_all(html_1)

It finds all tables (from table 1 to 7). What is a different? Why second solution doesn't work? I want to use only table nr 7 and make it into a function, so I need to second solution work properly. All code:

from bs4 import BeautifulSoup
import requests

r=requests.get("http://www.filmweb.pl/serial/Synowie+Anarchii-2008-479538/episodes#sezon7")
soup=BeautifulSoup(r.content)
html_1=("table",{"class":"awT votegroup votegroup7 wH episodesList"}, {"id":"sezon7"})
g_data=soup.find_all(html_1)
print g_data

Upvotes: 0

Views: 191

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124978

You are passing in the arguments for the search incorrectly. You'd have to combine those into one dictionary, and not use a tuple:

soup.find_all("table", {"class":"awT votegroup votegroup7 wH episodesList", "id":"sezon7"})

This works, but only just; the class is matched against a list (as the order of classes in the class attribute doesn't matter); you don't need it here as you already have an id to match on.

Since you are looking for just the one table, use soup.find():

soup.find("table", id="sezon7")

Your search was essentially looking for any element whose name was either table or {"class":"awT votegroup votegroup7 wH episodesList"} or {"id":"sezon7"}, as you only passed in a tuple for the name filter.

If you wanted to wrap this into a function, you can just use variables instead of string literals:

element_type = 'table'
element_id = 'sezon7'
soup.find(element_type, id=element_id)

Upvotes: 3

Related Questions