Skip to content

Extrair textos e links #14

Description

@AschRJ

import requests
from bs4 import BeautifulSoup
import pandas as pd

URL = "https://www.roendolivros.com.br/"

headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/140.0 Safari/537.36"
)
}

response = requests.get(URL, headers=headers, timeout=30)

if response.status_code == 429:
raise Exception(
"O site retornou HTTP 429 (Too Many Requests). "
"Aguarde alguns minutos antes de tentar novamente."
)

response.raise_for_status()

soup = BeautifulSoup(response.text, "html.parser")

dados = []

Captura os links encontrados na página

for link in soup.find_all("a", href=True):
texto = link.get_text(" ", strip=True)
url = link["href"]

if texto:
    dados.append({
        "Texto": texto,
        "URL": url
    })

Cria a planilha

df = pd.DataFrame(dados)

df.to_excel("roendo_livros.xlsx", index=False)

print(f"{len(df)} registros salvos em roendo_livros.xlsx")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions