使用BeautifulSoup获取标签样式
我正在抓取页面,并从该页面上的表中获取所有<tr>
元素,如下所示:
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>
标签的样式,但是我不知道如何。例如,如果我这样做:
for item in tr[0]: print item
它显然只是打印<td> ... </td>
东西。我想我大概可以做这样的事情print
tr[0].something,比如tr[0].tag
,但一切到目前为止,我已经尝试并没有带来我想要的东西。
回答:
只需使用以下属性即可访问tag["attribute"]
:
In [28]: soup = BeautifulSoup('<tr style="pretty"></tr>', 'html.parser')In [29]: print soup.find("tr")["style"]
pretty
如果您只想要具有样式属性an的tr标记来获取全部标记:
trs = s.find("table", class_="example-table").find_all("tr", style=True)for tr in trs:
print(tr["style"])
或使用CSS选择器:
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
以上是 使用BeautifulSoup获取标签样式 的全部内容, 来源链接: utcz.com/qa/418393.html