import requests from bs4 import BeautifulSoup import json def scrape_tales_by_year(start_year, end_year): base_url = "https://scp-wiki.wikidot.com/tales-by-date-" all_tales = [] for year in range(start_year, end_year + 1): url = f"{base_url}{year}" response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') # Find all tables, then find all table rows within each table for table in soup.find_all('table', class_='wiki-content-table'): for row in table.find_all('tr'): cells = row.find_all('td') if cells: a_tag = cells[0].find('a', href=True) if a_tag: tale_url = f"https://scp-wiki.wikidot.com{a_tag['href']}" tale_response = requests.get(tale_url) tale_soup = BeautifulSoup(tale_response.content, 'html.parser') # Extract the text from the div with id 'page-content' page_content = tale_soup.find('div', id='page-content') if page_content: # Remove the first div with specific style if it exists first_div = page_content.find('div', style="text-align: right;") if first_div: first_div.decompose() # Remove the div with class 'licensebox' if it exists licensebox_div = page_content.find('div', class_='licensebox') if licensebox_div: licensebox_div.decompose() # Now get the text from the rest of the page-content tale_text = page_content.get_text().strip() all_tales.append({'text': tale_text}) # Find the first non-empty line in the tale's text first_line = next((line for line in tale_text.splitlines() if line.strip()), "") print(first_line) # Print the first non-empty line of the tale # Optionally, write tale to a file or process it further here else: print(f"Could not find page-content div for {tale_url}") # Write the tales to a JSONL file with open('scp_tales.jsonl', 'w') as file: for tale in all_tales: json_record = json.dumps(tale) # tale is already a dict with the key "text" file.write(json_record + '\n') # Call the function with the range of years you want to scrape scrape_tales_by_year(2008, 2022)