Getting style of <tr> tag using BeautifulSoup

我的未来我决定 提交于 2020-01-28 11:27:10

问题


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 like print tr[0].something, like tr[0].tag, but everything I've tried so far hasn't resulted in what I want.


回答1:


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


来源:https://stackoverflow.com/questions/37843903/getting-style-of-tr-tag-using-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!