获取 <tr> 的样式使用 BeautifulSoup 标记 [英] Getting style of <tr> tag using 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
它显然只是打印 it obviously just prints the 只需使用 Just access the attribute using 如果你只想要带有样式属性的 tr 标签来获取它们: If you only want the tr tags with style attributes an to get them all: 或者使用 css 选择器: Or using a css selector: 使用您的实际网址: 这篇关于获取 <tr> 的样式使用 BeautifulSoup 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!... </td> 东西.我想我可能可以做一些类似 print tr[0].something
的事情,比如 tr[0].tag
,但到目前为止我尝试过的一切都没有达到了我想要的效果.
<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"]
访问属性:tag["attribute"]
:In [28]: soup = BeautifulSoup('<tr style="pretty"></tr>', 'html.parser')
In [29]: print soup.find("tr")["style"]
pretty
trs = s.find("table", class_="example-table").find_all("tr", style=True)
for tr in trs:
print(tr["style"])
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
登录
关闭