【问题标题】:Send Discord Embed of Product Stock Parsed form HTML with BS4 and Requests使用 BS4 和请求发送不和谐嵌入的产品库存解析表单 HTML
【发布时间】:2020-02-16 23:39:08
【问题描述】:

所以我有代码:

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

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

谢谢:)

【问题讨论】:

    标签: beautifulsoup python-requests html-parsing discord.py


    【解决方案1】:

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

    Embed 的大小有一些限制(复制自 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
    

    【讨论】:

      【解决方案2】:

      这样追加:

                          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)
      

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 2012-09-26
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 2023-03-30
        • 2022-01-11
        • 1970-01-01
        • 2022-11-16
        相关资源
        最近更新 更多