I'm trying to make a discord bot that web scrapes a random Wikipedia article, but after you try it once, it repeats the same thing
Question:
As the title says, im trying to make a discord bot that webscrapes a random Wikipedia page. I’m currently having trouble as the Discord bot will repeat the same page after you run the command for the page once. I’m assuming this has something to do with a loop but im not sure.
import requests
import webbrowser
from bs4 import BeautifulSoup
import discord
import random
#-----------------------------
#Var's
TOKEN = '#Not gonna show'
wiki_url = "https://en.wikipedia.org/wiki/Special:Random"
article_page = requests.get(wiki_url)
client = discord.Client()
soup = BeautifulSoup(article_page.text, "html.parser")
article_title = soup.find(id='firstHeading')
#---------------------------------------------------------------------------------
#Func's
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = (message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if message.channel.name == 'bot':
if user_message.lower() == 'hello wikibot':
await message.channel.send(f'Hello {username}!')
return
elif user_message.lower == 'bye wikibot':
await message.channel.send(f'See you later {username}!')
while user_message.lower() == '!random wiki':
response = f'The title of the Random article is:, {article_title.string}'
await message.channel.send(response)
break
#---------------------------------------------------------------------------------
client.run(TOKEN)```
Answers:
I would change this:
while user_message.lower() == '!random wiki':
response = f'The title of the Random article is:, {article_title.string}'
await message.channel.send(response)
break
Into:
elif user_message.lower() == "!random wiki":
article_page = requests.get(wiki_url)
soup = BeautifulSoup(article_page.text, "html.parser")
article_title = soup.find(id='firstHeading')
response = f'The title of the Random article is:, {article_title.string}'
await message.channel.send(response)
That way, you’re getting a new random page each time the user message is !random wiki
.
I would also remove the original article_page
, soup
and article_title
variables from the top of your code.
As the title says, im trying to make a discord bot that webscrapes a random Wikipedia page. I’m currently having trouble as the Discord bot will repeat the same page after you run the command for the page once. I’m assuming this has something to do with a loop but im not sure.
import requests
import webbrowser
from bs4 import BeautifulSoup
import discord
import random
#-----------------------------
#Var's
TOKEN = '#Not gonna show'
wiki_url = "https://en.wikipedia.org/wiki/Special:Random"
article_page = requests.get(wiki_url)
client = discord.Client()
soup = BeautifulSoup(article_page.text, "html.parser")
article_title = soup.find(id='firstHeading')
#---------------------------------------------------------------------------------
#Func's
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = (message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if message.channel.name == 'bot':
if user_message.lower() == 'hello wikibot':
await message.channel.send(f'Hello {username}!')
return
elif user_message.lower == 'bye wikibot':
await message.channel.send(f'See you later {username}!')
while user_message.lower() == '!random wiki':
response = f'The title of the Random article is:, {article_title.string}'
await message.channel.send(response)
break
#---------------------------------------------------------------------------------
client.run(TOKEN)```
I would change this:
while user_message.lower() == '!random wiki':
response = f'The title of the Random article is:, {article_title.string}'
await message.channel.send(response)
break
Into:
elif user_message.lower() == "!random wiki":
article_page = requests.get(wiki_url)
soup = BeautifulSoup(article_page.text, "html.parser")
article_title = soup.find(id='firstHeading')
response = f'The title of the Random article is:, {article_title.string}'
await message.channel.send(response)
That way, you’re getting a new random page each time the user message is !random wiki
.
I would also remove the original article_page
, soup
and article_title
variables from the top of your code.