-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
45 lines (32 loc) · 1.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
import pandas as pd
from bs4 import BeautifulSoup
import discord
from discord.ext import commands
def scrape_table_to_csv(url, class_name, output_filename='financial_data.csv'):
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find("table", class_=class_name)
if table is None:
print(f"No table found with class name '{class_name}'.")
return
header = table.find_all("th")
titles = [i.text.strip() for i in header]
df = pd.DataFrame(columns=titles)
rows = table.find_all("tr")
for i in rows[1:]:
data = i.find_all("td")
row = [tr.text.strip() for tr in data]
df.loc[len(df)] = row
df.to_csv(output_filename, index=False)
print(f"Data saved to {output_filename}")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
@bot.command()
async def scrape(ctx, url, class_name):
scrape_table_to_csv(url,class_name)
await ctx.send(file=discord.File(r'./financial_data.csv'))
# or:
#Put the Token here
bot.run("")