获取 <tr> 的样式使用 BeautifulSoup 标记 [英] Getting style of <tr> tag using BeautifulSoup

查看:20
本文介绍了获取 <tr> 的样式使用 BeautifulSoup 标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取一个页面,然后从该页面上的表格中获取所有 <tr> 元素,如下所示:

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]

输出:

<tr style="background-color:#C6EFCE"><td>...</td> ... <td>...</td></tr>

现在我正在尝试获取 <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

它显然只是打印... </td> 东西.我想我可能可以做一些类似 print tr[0].something 的事情,比如 tr[0].tag,但到目前为止我尝试过的一切都没有达到了我想要的效果.

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.

推荐答案

只需使用 tag["attribute"] 访问属性:

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

如果你只想要带有样式属性的 tr 标签来获取它们:

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"])

或者使用 css 选择器:

Or using a css selector:

trs = s.select("table.example-table tr[style]")

for tr in trs:
    print(tr["style"])

使用您的实际网址:

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

这篇关于获取 &lt;tr&gt; 的样式使用 BeautifulSoup 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆