Spaces:
Sleeping
Sleeping
| from bs4 import BeautifulSoup | |
| import pandas as pd | |
| # Open and read the HTML file | |
| with open("now_pac.html", 'r', encoding='utf-8') as file: | |
| html_content = file.read() | |
| # Parse the HTML content | |
| soup = BeautifulSoup(html_content, 'html.parser') | |
| # Find all article elements | |
| divs = soup.find_all('div', class_='image-slide-title') | |
| # Initialize a list to store the data | |
| data = [] | |
| for div in divs: | |
| name = div.text.strip().split(",")[0] | |
| office = div.text.strip().split(",")[1] | |
| # Append the extracted data to the list | |
| data.append({'Candidate': name, 'Office': office}) | |
| # Add to table | |
| df = pd.DataFrame(data) | |
| df["Endorsed by"] = "NOW PAC" | |
| # write to csv | |
| df.to_csv("now_endorsements.csv", index = False) |