Write me a python script which fuzzily grabs the body text and title etc (important…
- User
Write me a python script which fuzzily grabs the body text and title etc (important content) of a page for storing in a markdown file
- Assistant
To achieve this, you can use Python libraries such as requests, BeautifulSoup, and goose3 to fetch the webpage, parse the HTML, and extract the important content. Install the required libraries using pip:
pip install requests beautifulsoup4 goose3Here's a Python script that fetches the important content from a given URL and stores it in a Markdown file:
import requests from bs4 import BeautifulSoup from goose3 import Goose import re def get_title(soup): if soup.title: return soup.title.string.strip() return "" def clean_text(text): text = re.sub(r'\n\s*\n', '\n\n', text) return text.strip() def extract_content(url): response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') g = Goose() article = g.extract(url=url) title = get_title(soup) body = clean_text(article.cleaned_text) return title, body def save_to_markdown(title, body, filename): with open(filename, 'w', encoding='utf-8') as f: f.write(f'# {title}\n\n') f.write(body) def main(): url = input("Enter the URL of the webpage: ") title, body = extract_content(url) filename = input("Enter the name of the Markdown file to save (e.g., output.md): ") save_to_markdown(title, body, filename) print(f"Content successfully saved to {filename}") if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.