Another practice at using beautiful soup (with a little help from ChatGPT).
Extracting lottery numbers from this website: https://www.national-lottery.co.uk/results/lotto/draw-history
import pandas as pd
import matplotlib.pyplot as plt
import requests
from bs4 import BeautifulSoup
import re
import matplotlib.pyplot as plt
from collections import Counter
url = 'https://www.national-lottery.co.uk/results/lotto/draw-history'
lottery_site = requests.get(url)
soup = BeautifulSoup(lottery_site.content, "html.parser")
# print(soup.prettify())
ball_number_elements = soup.find_all("span", class_="table_cell_block")
ball_numbers = []
for element in ball_number_elements:
numbers_str = element.get_text(strip=True)
if "-" in numbers_str:
numbers = [int(num.strip()) for num in numbers_str.split(" - ")]
ball_numbers.append(numbers)
# print(ball_numbers)
flat_numbers_list = [number for sublist in ball_numbers for number in sublist]
# print(flat_numbers_list)
number_counts = Counter(flat_numbers_list)
sorted_numbers = sorted(number_counts.items(), key=lambda x: x[1], reverse=True)
# print(sorted_numbers)
plt.figure(figsize=(10, 6))
plt.bar([str(num) for num, freq in sorted_numbers], [freq for num, freq in sorted_numbers], color='skyblue')
plt.xlabel('Lottery Number')
plt.ylabel('Frequency')
plt.title('The Most Common Lottery Numbers From the Past 6 Months')
plt.xticks(rotation=90)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()