【发布时间】:2021-06-01 16:33:55
【问题描述】:
使用beautifulsoup 抓取一个网站(这里以这个人为例:https://rocket-league.com/player/Guuter),我已经设法得到了我想要的文本。但是,我不知道如何正确地将每个字符串分配给某些变量,以便正确格式化它们。这是一个交易网站,所以我想将交易分为“有”和“想要”部分
到目前为止我所取得的成就(顺便说一句,包括巨大的差距):
Has:
#first item
Titanium White #colour
Credits #item
200 #amount
#second item
Titanium White #colour
Credits #item
250 #amount
Wants:
Titanium White #colour
Neo-Thermal #item
#no amount this time - therefore is 1
#second item
Grey #colour
Halo #item
#once again no amount - therefore is 1
这是一个不和谐的机器人,当你用命令查找某人的名字时,它会给你他们的第一笔交易(想要更多 - 请让我知道如何做到这一点 - 不让我使用 find_all 没有错误)
@bot.command()
async def rlgarage(ctx, arg):
name = arg
#name = "Guuter"
page = requests.get("https://rocket-league.com/player/"+name)
soup = BeautifulSoup(page.content, 'html.parser')
tradeitems = soup.find(class_="rlg-trade__items")
haveitems = tradeitems.find(class_="rlg-trade__itemshas")
haveitemstext = haveitems.get_text()
wantitems = tradeitems.find(class_="rlg-trade__itemswants")
wantitemstext = wantitems.get_text()
await ctx.send("Has:" + haveitemstext + "Wants:" + wantitemstext)
首选输出:
Has:
Titanium White Credits (200)
Titanium White Credits (250)
Wants:
Titanium White Neo-Thermal
Grey Halo
【问题讨论】:
标签: python beautifulsoup