Getting style of <tr> tag using BeautifulSoup
12,291
Just access the attribute using tag["attribute"]
:
In [28]: soup = BeautifulSoup('<tr style="pretty"></tr>', 'html.parser')
In [29]: print soup.find("tr")["style"]
pretty
If you only want the tr tags with style attributes an to get them all:
trs = s.find("table", class_="example-table").find_all("tr", style=True)
for tr in trs:
print(tr["style"])
Or using a css selector:
trs = s.select("table.example-table tr[style]")
for tr in trs:
print(tr["style"])
Using your actual url:
In [41]: r = requests.get("http://lol.esportswikis.com/wiki/G2_Esports/Match_History")
In [42]: s = BeautifulSoup(r.content, "lxml")
In [43]: trs = s.select("table.wikitable.sortable tr[style]")
In [44]:
In [44]: for tr in trs:
....: print(tr["style"])
....:
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#C6EFCE
background-color:#FFC7CE
background-color:#FFC7CE
background-color:#C6EFCE
Author by
user5368737
Updated on June 04, 2022Comments
-
user5368737 almost 2 years
I'm scraping a page and from a table on that page I'm getting all
<tr>
elements like so:r = requests.get("http://lol.esportswikis.com/wiki/G2_Esports/Match_History") s = BeautifulSoup(r.content, "lxml") tr = s.find_all("table", class_="wikitable sortable")[0].find_all("tr")[3:] print tr[0]
which outputs:
<tr style="background-color:#C6EFCE"><td>...</td> ... <td>...</td></tr>
Now I'm trying to get the style of the
<tr>
tag, but I have no idea how. If I do this for example:for item in tr[0]: print item
it obviously just prints the
<td> ... </td>
stuff. I'm thinking I can probably do something likeprint tr[0].something
, liketr[0].tag
, but everything I've tried so far hasn't resulted in what I want.