Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

python - Getting style of <tr> tag using BeautifulSoup

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...