beautifulsoup find_all 标题

html 是


<div class="trn-defstat__value">

    <img src="https://trackercdn.com/rainbow6-ubi/assets/images/badge-ash.16913d82e3.png" title="ASH" style="height:    35px; padding-right: 8px;"> 

    <img src="https://trackercdn.com/rainbow6-ubi/assets/images/badge-jager.600b2773be.png" title="JÄGER"   style="height: 35px; padding-right: 8px;">

    <img src="https://trackercdn.com/rainbow6-ubi/assets/images/badge-bandit.385144d970.png" title="BANDIT"     style="height: 35px; padding-right: 8px;">

</div>

我想获得每个标题的值。


但在此之前,我是这样写的


from bs4 import BeautifulSoup as bs

import requests



bsURL = "https://r6.tracker.network/profile/pc/Spoit.GODSENT"

respinse = requests.get(bsURL)

html = bs(respinse.text, 'html.parser')



title = html.find_all(class_='trn-defstat__value')[4]


print(title)

结果->


<div class="trn-defstat__value">

<img src="https://trackercdn.com/rainbow6-ubi/assets/images/badge-ash.16913d82e3.png" style="height: 35px; padding-right: 8px;" title="ASH"/>

<img src="https://trackercdn.com/rainbow6-ubi/assets/images/badge-jager.600b2773be.png" style="height: 35px; padding-right: 8px;" title="JÄGER"/>

<img src="https://trackercdn.com/rainbow6-ubi/assets/images/badge-bandit.385144d970.png" style="height: 35px; padding-right: 8px;" title="BANDIT"/>

</div>

我应该怎么办?


红糖糍粑
浏览 138回答 3
3回答

慕尼黑的夜晚无繁华

该脚本将打印<img>“顶级操作员”部分的所有标题:from bs4 import BeautifulSoup as bsimport requestsbsURL = "https://r6.tracker.network/profile/pc/Spoit.GODSENT"respinse = requests.get(bsURL)html = bs(respinse.text, 'html.parser')# find Top Operators tagoperators = html.find(class_='trn-defstat__name', text='Top Operators')for img in operators.find_next('div').find_all('img'):&nbsp; &nbsp; print(img['title'])印刷:ASHJÄGERBANDIT或者使用CSS:for img in html.select('.trn-defstat__name:contains("Top Operators") + * img'):&nbsp; &nbsp; print(img['title'])

忽然笑

只需使用.get()函数获取属性并传入属性名称即可。pip install html5lib我建议你使用它,我相信它是一个更好的解析器。from bs4 import BeautifulSoup as bs&nbsp;import requests&nbsp; &nbsp;bsURL = "https://r6.tracker.network/profile/pc/Spoit.GODSENT"&nbsp;respinse = requests.get(bsURL)&nbsp;html = bs(respinse.content, 'html5lib')&nbsp; &nbsp;container = html.find("div", class_= "trn-defstat mb0 top-operators")imgs = container.find_all("img")for img in imgs:&nbsp; &nbsp; &nbsp;print(img.get("title"))我似乎不明白您想要抓取网站的哪一部分,但请注意有时会先获取blockhtml 代码,其中包含您想要抓取的详细信息:)

不负相思意

只需使用.get()函数获取属性并传入属性名称即可。pip install html5lib我建议你使用它,我相信它是一个更好的解析器。from bs4 import BeautifulSoup as bs&nbsp;import requests&nbsp; &nbsp;bsURL = "https://r6.tracker.network/profile/pc/Spoit.GODSENT"&nbsp;respinse = requests.get(bsURL)&nbsp;html = bs(respinse.content, 'html5lib')&nbsp; &nbsp;container = html.find("div", class_= "trn-defstat mb0 top-operators")imgs = container.find_all("img")for img in imgs:&nbsp; &nbsp; &nbsp;print(img.get("title"))我似乎不明白您想要抓取网站的哪一部分,但请注意有时会先获取blockhtml 代码,其中包含您想要抓取的详细信息:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python