Scrape text and article content
For the text of one element, use get_text(" ", strip=True) in Beautiful Soup or html_text2() in rvest. For the main text of an article, without menus, ads and footers:
-
Use the page's own structure
Many sites wrap the article in
<article>or<main>; select that and removescript,style,navandasideinside it. -
Use an extraction library
When you scrape many different sites. trafilatura (2.3.1, Apache-2.0) is a Python package and command-line tool that extracts the main text and metadata such as title, author and date from a page.
# pip install trafilatura
import trafilatura
downloaded = trafilatura.fetch_url("https://example.com/some-article")
print(trafilatura.extract(downloaded, output_format="markdown", with_metadata=True))