如何将包含从网站解析的多个链接的嵌入消息发送到 Webhook?

Flo*_*ian 6 python beautifulsoup webhooks discord discord.py

我希望我的嵌入消息看起来像这样,但我的只返回一个链接。

在此处输入图片说明

这是我的代码:

import requests
from bs4 import BeautifulSoup
from discord_webhook import DiscordWebhook, DiscordEmbed

url = 'https://www.solebox.com/Footwear/Basketball/Lebron-X-JE-Icon-QS-variant.html'
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
for tag in soup.find_all('a', class_="selectSize"):
    #There's multiple 'id' resulting in more than one link
    aid = tag.get('id')
    #There's also multiple sizes
    size = tag.get('data-size-us')
    #These are the links that need to be shown in the embed message
    product_links = "https://www.solebox.com/{0}".format(aid)

webhook = DiscordWebhook(url='WebhookURL')
embed = DiscordEmbed(title='Title')
embed.set_author(name='Brand')
embed.set_thumbnail(url="Image")
embed.set_footer(text='Footer')
embed.set_timestamp()
embed.add_embed_field(name='Sizes', value='US{0}'.format(size))
embed.add_embed_field(name='Links', value='[Links]({0})'.format(product_links))
webhook.add_embed(embed)
webhook.execute()
Run Code Online (Sandbox Code Playgroud)

小智 1

这很可能会给你带来你想要的结果。 是一个字符串,这意味着 for 循环中的每次迭代都只是用新字符串type(product_links)重写变量。product_links如果您在循环之前声明一个列表并将其附加product_links到该列表,那么它很可能会产生您想要的结果。

注意:我必须使用该网站的不同 URL。问题中指定的那个不再可用。我还必须使用不同的标头,因为提问者不断向我提供 403 错误。

附加说明:通过代码逻辑返回的 URLS 返回不通向任何地方的链接。我觉得你需要解决这个问题,因为我不知道你到底想做什么,但是我觉得这回答了为什么你只得到一个链接的问题。

import requests
from bs4 import BeautifulSoup

url = 'https://www.solebox.com/Footwear/Basketball/Air-Force-1-07-PRM-variant-2.html'

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3"}

r = requests.get(url=url, headers=headers) 

soup = BeautifulSoup(r.content, "lxml")

product_links = [] # Create our product

for tag in soup.find_all('a', class_="selectSize"):
    #There's multiple 'id' resulting in more than one link
    aid = tag.get('id')
    #There's also multiple sizes
    size = tag.get('data-size-us')
    #These are the links that need to be shown in the embed message
    product_links.append("https://www.solebox.com/{0}".format(aid))
Run Code Online (Sandbox Code Playgroud)