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

时间:2019-10-14 23:02:54

标签: python beautifulsoup discord webhooks discord.py

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

enter image description here

这是我的代码:

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()

1 个答案:

答案 0 :(得分:0)

这很可能会为您带来所需的结果。 type(product_links)是一个字符串,这意味着for循环中的每个迭代都只是使用新的字符串重写product_links变量。如果您在循环之前声明一个List并将product_links附加到该列表,则很可能会导致您想要的结果。

注意:我必须使用与该站点不同的URL。问题中指定的一个不再可用。我还不得不使用一个不同的标头,作为问询者不断提出的标头,不断给我带来403错误。

附加说明:通过您的代码逻辑返回的URL返回的链接指向无处。我觉得您需要通过一个步骤来完成工作,因为我不知道您到底要做什么,但是我觉得这回答了为什么您只能获得一个链接的问题。

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))