使用 BS4 和请求发送产品库存解析形式 HTML 的 Discord 嵌入

Send Discord Embed of Product Stock Parsed form HTML with BS4 and Requests

所以我有代码:

import requests
import discord
from bs4 import BeautifulSoup

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message):
        if (message.channel.id == 678447420643868674):
                if "test" in message.content:
                    r = requests.get('https://www.jimmyjazz.com/mens/footwear/adidas-solar-hu-nmd/BB9528')
                    soup = BeautifulSoup(r.text, 'html.parser')
                    embed = discord.Embed(color=0x00ff00)
                    embed.title = "test" 
                    for anchor_tag in soup.find_all(class_="box_wrapper")[0].findChildren():
                        if "piunavailable" in anchor_tag['class']:
                            embed.description = f"Size {anchor_tag.text} OOS"
                            await message.channel.send(embed=embed)
                        else:
                            embed.description = f"Size {anchor_tag.text}  in stock!"
                            await message.channel.send(embed=embed)
client = MyClient()
client.run('NjY2MDMyMDc0NjM3MTgwOTQ4.XkjBLg.I3dtsL2nkVh_bafTlycSwBApQfU')

然后将商品库存作为每个尺寸的嵌入件发送: https://gyazo.com/7a7c868d00a99fc3798a3c24feb9ea7e

我如何更改代码以使其在一个嵌入中发送每个尺寸,而不是按尺寸发送一个嵌入?

谢谢:)

discord 中的嵌入可以包含可以使用 embed.add_field() 函数添加的字段 embed.add_field(name="Field1", value="hi", inline=False)

嵌入有一些大小限制(从 https://discordjs.guide/popular-topics/embeds.html#notes 复制):

  • 字段名称限制为 256 个字符,其值限制为 1024 个字符
  • 最多可以有 25 个字段
  • 此外,嵌入结构中所有字符的总和不得超过6000个字符

因此,当它超过 25 个字段或 6000 个字符时,您可能必须将您的产品库存拆分为多个嵌入,方法是为两者设置一个计数器,如果超过重置和发送消息。

这是部分示例(我没有测试过,但逻辑应该是正确的)

r = requests.get('https://www.jimmyjazz.com/mens/footwear/adidas-solar-hu-nmd/BB9528')
soup = BeautifulSoup(r.text, 'html.parser')
charCount = 0
fieldCount = 0
embed = discord.Embed(color=0x00ff00)
embed.title = "test"
for anchor_tag in soup.find_all(class_="box_wrapper")[0].findChildren():
    anchor_text = anchor_tag.text
    charCount += len(anchor_text)

    if charCount >=6000 or fieldCount >=25:
        charCount = len(anchor_text)
        fieldCount = 0

        await message.channel.send(embed=embed)
        embed = discord.Embed(color=0x00ff00)
        embed.title = "test"

    if "piunavailable" in anchor_tag['class']:
        embed.add_field(name= f"Size {anchor_text}", value="Out Of Stock")
    else:
        embed.add_field(name= f"Size {anchor_text}", value="In stock!")

    fieldCount +=1

这样追加:

                    for anchor_tag in soup.find_all(class_="box_wrapper")[0].findChildren():
                        if "piunavailable" in anchor_tag['class']:
                            embed.add_field(name= f"Size {anchor_tag.text}", value=":x:", inline= 'true')
                        else:
                            embed.add_field(name= f"Size {anchor_tag.text}", value=f":white_check_mark: | ATC", inline= 'true')
                    await message.channel.send(embed=embed)