3 回答

TA贡献1864条经验 获得超6个赞
该脚本将打印<img>“顶级操作员”部分的所有标题:
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')
# find Top Operators tag
operators = html.find(class_='trn-defstat__name', text='Top Operators')
for img in operators.find_next('div').find_all('img'):
print(img['title'])
印刷:
ASH
JÄGER
BANDIT
或者使用CSS:
for img in html.select('.trn-defstat__name:contains("Top Operators") + * img'):
print(img['title'])

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

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