Cra*_*aig 4 python beautifulsoup discord
首先我要说的是我对 Python 很陌生。我一直在使用 discord.py 和 Beautiful Soup 4 构建一个 Discord 机器人。这就是我现在的位置:
@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!")
Run Code Online (Sandbox Code Playgroud)
现在,我尝试使用get_text()多种不同的方式从这段代码中删除括号和 HTML 标签,但每次都会抛出错误。我如何才能实现这一目标或将这些数据输出到数组或列表中,然后只打印纯文本?
代替
text = soupObject.find_all("font", attrs={'size': '4'})
Run Code Online (Sandbox Code Playgroud)
有了这个:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11367 次 |
| 最近记录: |