有没有办法让我的 discord 机器人访问 tenor 网站,然后将特定标签下的所有 gif 添加到它的输出中?
is there a way for my discord bot to access the tenor website and then just add all gifs under a certain tag to it's output?
标题说明了一切。我有一个 discord 机器人,只要使用某个关键字或命令,它基本上就会上传 cat gif。使用我当前的代码,我必须手动将 tenor/gif link 添加到输出集中,以便它可以显示该 gif。相反,我希望机器人只 post 来自 tenor 或任何其他 gif 网站的任何猫的 gif。我很确定这些网站有一个标签功能,例如将标签“猫”分配给猫 gif。我想知道哪个 gif 被标记为 cat,然后将该 gif 添加到它的输出集中。我有办法做到这一点吗?
import discord
import os
import random
client = discord.Client()
cat_pictures = ["cats", "cat"]
cat_encouragements = [
"https://tenor.com/view/dimden-cat-cute-cat-cute-potato-gif-20953746", "https://tenor.com/view/dimden-cute-cat-cute-cat-potato-gif-20953747", "https://tenor.com/view/cute-cat-cute-cat-dimden-gif-19689251", "https://tenor.com/view/dimden-cute-cat-cute-cat-potato-gif-21657791",
"https://tenor.com/view/cats-kiss-gif-10385036",
"https://tenor.com/view/cute-kitty-best-kitty-alex-cute-pp-kitty-omg-yay-cute-kitty-munchkin-kitten-gif-15917800",
"https://tenor.com/view/cute-cat-oh-yeah-awesome-cats-amazing-gif-15805236",
"https://tenor.com/view/cat-broken-cat-cat-drinking-cat-licking-cat-air-gif-20661740",
"https://tenor.com/view/funny-animals-cute-chicken-cat-fight-dinner-time-gif-8953000"]
@client.event
async def on_ready():
print('We have logged in as catbot '.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith ('!help'):
await message.channel.send('''
I only have two commands right now which are !cat which posts an image of a cat. !cats which gives you a video/gif
''')
if any(word in message.content for word in cat_pictures):
await message.channel.send(random.choice(cat_encouragements))
if any(word in message.content for word in cat_apology):
await message.channel.send(random.choice(cat_sad))
if any(word in message.content for word in cat_dog):
await message.channel.send(random.choice(cat_dogs))
client.run(os.getenv('TOKEN'))
如果你想从其他页面获取数据,那么你必须学习如何"scrape"
。
对于某些页面,您可能需要使用 requests
(或 urllib
)从服务器获取 HTML,并使用 beautifulsoup
(或 lxml
)获取在 HTML 中搜索数据。通常页面使用 JavaScript
来添加元素,因此它可能需要 Selenium 来控制真正的网络浏览器,它可以 运行 JavaScript (因为 requests
, urllib
,beautifulsoup
,lxml
不能运行JavaScript
)
但首先你应该检查页面是否有 API
以便开发人员以更简单的方式获取数据 - 作为 JSON 数据 - 这样你就不必在 HTML 中搜索。
@ChrisDoyle 注意到有 tensor API.
的文档
本文档甚至在 Python(使用 requests
)中显示了获取 JSON 数据的示例。示例可能只需要显示如何从 JSON 获取 url,因为还有其他信息 - 例如图像大小、gif、小 gif、动画 gif、mp4 等
这是我基于文档中示例的版本
import requests
# set the apikey and limit
API_KEY = "LIVDSRZULELA" # test value
search_term = "cat"
def get_urls(search, limit=8):
payload = {
'key': API_KEY,
'limit': limit,
'q': search,
}
# our test search
# get the top 8 GIFs for the search term
r = requests.get("https://g.tenor.com/v1/search", params=payload)
results = []
if r.status_code == 200:
data = r.json()
#print('[DEBUG] data:', data)
for item in data['results']:
#print('[DEBUG] item:', item)
for media in item['media']:
#print('[DEBUG] media:', media)
#for key, value in media.items():
# print(f'{key:10}:', value['url'])
#print('----')
if 'tinygif' in media:
results.append(media['tinygif']['url'])
else:
results = []
return results
# --- main ---
cat_encouragements = get_urls('cat')
for url in cat_encouragements:
print(url)
直接给tiny gif
张图片提供网址
https://media.tenor.com/images/eff22afc2220e9df92a7aa2f53948f9f/tenor.gif
https://media.tenor.com/images/e0f28542d811073f2b3d223e8ed119f3/tenor.gif
https://media.tenor.com/images/75b3c8eca95d917c650cd574b91db7f7/tenor.gif
https://media.tenor.com/images/80aa0a25bee9defa1d1d7ecaab75f3f4/tenor.gif
https://media.tenor.com/images/042ef64f591bdbdf06edf17e841be4d9/tenor.gif
https://media.tenor.com/images/1e9df4c22da92f1197b997758c1b3ec3/tenor.gif
https://media.tenor.com/images/6562518088b121eab2d19917b65ee793/tenor.gif
https://media.tenor.com/images/eafc0f0bef6d6fd135908eaba24393ac/tenor.gif
如果您取消注释代码中的某些 print()
,您可能会看到更多信息。
例如来自 media.items()
的单个图像的链接
nanowebm : https://media.tenor.com/videos/513b211140bedc05d5ab3d8bc3456c29/webm
tinywebm : https://media.tenor.com/videos/7c1777a988eedb267a6b7d7ed6aaa858/webm
mp4 : https://media.tenor.com/videos/146935e698960bf723a1cd8031f6312f/mp4
loopedmp4 : https://media.tenor.com/videos/e8be91958367e8dc4e6a079298973362/mp4
nanomp4 : https://media.tenor.com/videos/4d46f8b4e95a536d2e25044a0a288968/mp4
tinymp4 : https://media.tenor.com/videos/390f512fd1900b47a7d2cc516dd3283b/mp4
tinygif : https://media.tenor.com/images/eff22afc2220e9df92a7aa2f53948f9f/tenor.gif
mediumgif : https://media.tenor.com/images/c90bf112a9292c442df9310ba5e140fd/tenor.gif
nanogif : https://media.tenor.com/images/6f6eb54b99e34a8128574bd860d70b2f/tenor.gif
gif : https://media.tenor.com/images/8ab88b79885ab587f84cbdfbc3b87835/tenor.gif
webm : https://media.tenor.com/videos/926d53c9889d7604da6745cd5989dc3c/webm
在代码中,我使用文档中的 API_KEY = "LIVDSRZULELA"
,但您应该在页面上注册以获得您的独特 API_KEY
。
通常 API_KEYs
文档中的内容可能有限制或始终生成相同的数据 - 它们仅为测试而创建,不用于实际应用。
文档显示了更多获取和过滤图像的方法 - 即获取 trending images
.
标题说明了一切。我有一个 discord 机器人,只要使用某个关键字或命令,它基本上就会上传 cat gif。使用我当前的代码,我必须手动将 tenor/gif link 添加到输出集中,以便它可以显示该 gif。相反,我希望机器人只 post 来自 tenor 或任何其他 gif 网站的任何猫的 gif。我很确定这些网站有一个标签功能,例如将标签“猫”分配给猫 gif。我想知道哪个 gif 被标记为 cat,然后将该 gif 添加到它的输出集中。我有办法做到这一点吗?
import discord
import os
import random
client = discord.Client()
cat_pictures = ["cats", "cat"]
cat_encouragements = [
"https://tenor.com/view/dimden-cat-cute-cat-cute-potato-gif-20953746", "https://tenor.com/view/dimden-cute-cat-cute-cat-potato-gif-20953747", "https://tenor.com/view/cute-cat-cute-cat-dimden-gif-19689251", "https://tenor.com/view/dimden-cute-cat-cute-cat-potato-gif-21657791",
"https://tenor.com/view/cats-kiss-gif-10385036",
"https://tenor.com/view/cute-kitty-best-kitty-alex-cute-pp-kitty-omg-yay-cute-kitty-munchkin-kitten-gif-15917800",
"https://tenor.com/view/cute-cat-oh-yeah-awesome-cats-amazing-gif-15805236",
"https://tenor.com/view/cat-broken-cat-cat-drinking-cat-licking-cat-air-gif-20661740",
"https://tenor.com/view/funny-animals-cute-chicken-cat-fight-dinner-time-gif-8953000"]
@client.event
async def on_ready():
print('We have logged in as catbot '.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith ('!help'):
await message.channel.send('''
I only have two commands right now which are !cat which posts an image of a cat. !cats which gives you a video/gif
''')
if any(word in message.content for word in cat_pictures):
await message.channel.send(random.choice(cat_encouragements))
if any(word in message.content for word in cat_apology):
await message.channel.send(random.choice(cat_sad))
if any(word in message.content for word in cat_dog):
await message.channel.send(random.choice(cat_dogs))
client.run(os.getenv('TOKEN'))
如果你想从其他页面获取数据,那么你必须学习如何"scrape"
。
对于某些页面,您可能需要使用 requests
(或 urllib
)从服务器获取 HTML,并使用 beautifulsoup
(或 lxml
)获取在 HTML 中搜索数据。通常页面使用 JavaScript
来添加元素,因此它可能需要 Selenium 来控制真正的网络浏览器,它可以 运行 JavaScript (因为 requests
, urllib
,beautifulsoup
,lxml
不能运行JavaScript
)
但首先你应该检查页面是否有 API
以便开发人员以更简单的方式获取数据 - 作为 JSON 数据 - 这样你就不必在 HTML 中搜索。
@ChrisDoyle 注意到有 tensor API.
的文档本文档甚至在 Python(使用 requests
)中显示了获取 JSON 数据的示例。示例可能只需要显示如何从 JSON 获取 url,因为还有其他信息 - 例如图像大小、gif、小 gif、动画 gif、mp4 等
这是我基于文档中示例的版本
import requests
# set the apikey and limit
API_KEY = "LIVDSRZULELA" # test value
search_term = "cat"
def get_urls(search, limit=8):
payload = {
'key': API_KEY,
'limit': limit,
'q': search,
}
# our test search
# get the top 8 GIFs for the search term
r = requests.get("https://g.tenor.com/v1/search", params=payload)
results = []
if r.status_code == 200:
data = r.json()
#print('[DEBUG] data:', data)
for item in data['results']:
#print('[DEBUG] item:', item)
for media in item['media']:
#print('[DEBUG] media:', media)
#for key, value in media.items():
# print(f'{key:10}:', value['url'])
#print('----')
if 'tinygif' in media:
results.append(media['tinygif']['url'])
else:
results = []
return results
# --- main ---
cat_encouragements = get_urls('cat')
for url in cat_encouragements:
print(url)
直接给tiny gif
张图片提供网址
https://media.tenor.com/images/eff22afc2220e9df92a7aa2f53948f9f/tenor.gif
https://media.tenor.com/images/e0f28542d811073f2b3d223e8ed119f3/tenor.gif
https://media.tenor.com/images/75b3c8eca95d917c650cd574b91db7f7/tenor.gif
https://media.tenor.com/images/80aa0a25bee9defa1d1d7ecaab75f3f4/tenor.gif
https://media.tenor.com/images/042ef64f591bdbdf06edf17e841be4d9/tenor.gif
https://media.tenor.com/images/1e9df4c22da92f1197b997758c1b3ec3/tenor.gif
https://media.tenor.com/images/6562518088b121eab2d19917b65ee793/tenor.gif
https://media.tenor.com/images/eafc0f0bef6d6fd135908eaba24393ac/tenor.gif
如果您取消注释代码中的某些 print()
,您可能会看到更多信息。
例如来自 media.items()
的单个图像的链接
nanowebm : https://media.tenor.com/videos/513b211140bedc05d5ab3d8bc3456c29/webm
tinywebm : https://media.tenor.com/videos/7c1777a988eedb267a6b7d7ed6aaa858/webm
mp4 : https://media.tenor.com/videos/146935e698960bf723a1cd8031f6312f/mp4
loopedmp4 : https://media.tenor.com/videos/e8be91958367e8dc4e6a079298973362/mp4
nanomp4 : https://media.tenor.com/videos/4d46f8b4e95a536d2e25044a0a288968/mp4
tinymp4 : https://media.tenor.com/videos/390f512fd1900b47a7d2cc516dd3283b/mp4
tinygif : https://media.tenor.com/images/eff22afc2220e9df92a7aa2f53948f9f/tenor.gif
mediumgif : https://media.tenor.com/images/c90bf112a9292c442df9310ba5e140fd/tenor.gif
nanogif : https://media.tenor.com/images/6f6eb54b99e34a8128574bd860d70b2f/tenor.gif
gif : https://media.tenor.com/images/8ab88b79885ab587f84cbdfbc3b87835/tenor.gif
webm : https://media.tenor.com/videos/926d53c9889d7604da6745cd5989dc3c/webm
在代码中,我使用文档中的 API_KEY = "LIVDSRZULELA"
,但您应该在页面上注册以获得您的独特 API_KEY
。
通常 API_KEYs
文档中的内容可能有限制或始终生成相同的数据 - 它们仅为测试而创建,不用于实际应用。
文档显示了更多获取和过滤图像的方法 - 即获取 trending images
.