微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

美丽的汤-从<ul>中的所有<li>元素获取文本

如何解决美丽的汤-从<ul>中的所有<li>元素获取文本

使用以下代码

match_url = f'https://interativos.globoesporte.globo.com/cartola-fc/mais-escalados/mais-escalados-do-cartola-fc'

browser.visit(match_url)
browser.find_by_tag('li[class="historico-rodadas__rodada historico-rodadas__rodada--ativa"]').click()

soup = BeautifulSoup(browser.html,'html.parser')
innerContent = soup.findAll('ul',class_="field__players")

print (innerContent)

我设法获取<ul>

[<ul class="field__players"><li class="player"...] 

enter image description here

现在如何为列表中的所有玩家访问player__nameplayer__value的文本?

解决方法

这对您有帮助:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome()

driver.get('https://interativos.globoesporte.globo.com/cartola-fc/mais-escalados/mais-escalados-do-cartola-fc')

src = driver.page_source

driver.close()

soup = BeautifulSoup(src,'html5lib')

innerContent = soup.find('ul',class_="field__players")

li_items = innerContent.find_all('li')

for li in li_items:
    p_tags = li.find_all('p')[:-1] #The [:-1] removes the last p tag from the list,which is player__label

    for p in p_tags:
        print(p.text)

输出:

Keno
2.868.755
Pedro
2.483.069
Bruno Henrique
1.686.894
Hugo Souza
809.186
Guilherme Arana
1.314.769
Filipe Luís
776.147
Thiago Galhardo
2.696.853
Vinícius
1.405.012
Nenê
1.369.209
Jorge Sampaoli
1.255.731
Réver
1.505.522
Víctor Cuesta
1.220.451
,

我应该把它放在这里告诉你他想要什么。

@State

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?