all获取文本作为列表

all获取文本作为列表

本文介绍了在BS4中使用find_all获取文本作为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我首先要说我是Python的新手.我一直在用discord.py和Beautiful Soup 4建立一个Discord机器人.这是我的位置:

I'll start by saying I'm very new with Python. I've been building a Discord bot with discord.py and Beautiful Soup 4. Here's where I'm at:

@commands.command(hidden=True)
async def roster(self):
    """Gets a list of CD's members"""
    url = "http://www.clandestine.pw/roster.html"
    async with aiohttp.get(url) as response:
        soupObject = BeautifulSoup(await response.text(), "html.parser")
    try:
        text = soupObject.find_all("font", attrs={'size': '4'})
        await self.bot.say(text)
    except:
        await self.bot.say("Not found!")

这是输出:

现在,我尝试以多种不同方式使用get_text()从该代码中删除括号和HTML标记,但每次都会引发错误.我将如何实现该目标或将这些数据输出到数组或列表中,然后仅打印纯文本?

Now, I've tried using get_text() in multiple different ways to strip the brackets and HTML tags from this code, but it throws an error each time. How would I be able to either achieve that or output this data into an array or list and then just print the plain text?

推荐答案

替换

text = soupObject.find_all("font", attrs={'size': '4'})

与此:

all_font_tags = soupObject.find_all("font", attrs={'size': '4'})
list_of_inner_text = [x.text for x in all_font_tags]
# If you want to print the text as a comma separated string
text = ', '.join(list_of_inner_text)

这篇关于在BS4中使用find_all获取文本作为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:23