我正在尝试使用BeautifulSoup
从网页获取数据。它对大多数数据都很好,但是一个类的工作方式似乎不同,我不知道该怎么做。评论是否会影响soup.find_all
因此,我有一个网页,其中有几个类具有相同的名称,我正在查找带有soup.find_all
的内容。而这适用于class "points column"
,它总是这样:
<div class="points column">Punkte</div>
<div class="points column">45.677</div>
<div class="points column">43.445</div>
...
它不适用于class "teamValue column"
,如下所示:
<div class="teamValue column">Teamwert</div>
<div class="teamValue column">
<!-- react-text: 690 -->
554,4
<!-- /react-text -->
<!-- react-text: 691 -->
€
<!-- /react-text -->
</div>
<div class="teamValue column">
<!-- react-text: 705 -->
449,7
<!-- /react-text -->
<!-- react-text: 706 -->
€
<!-- /react-text -->
</div>
...
这是我的密码:
def getplayerdata(self):
bot = self.bot
soup = BeautifulSoup(bot.page_source, 'html.parser')
playervalue = soup.find_all("div",class_="teamValue column",text=True)
playerpoints = soup.find_all("div",class_="points column",text=True)
print(playervalue)
print(playerpoints)
playerpoints
的输出按预期工作,我得到所有数据,并且只能用.string
命令提取文本
但是对于playervalue
,我的列表中只有一个元素,那就是:
[<div class="teamValue column">Teamwert</div>]
您可以使用soup.select和re.sub来删除新行
如果使用
find_all()
而不使用text=True
和.get_text()
或.text
而不是.string
,我可以得到这个文本结果:
只需更改
text= False
:)输出:
相关问题 更多 >
编程相关推荐