Skip to content

Instantly share code, notes, and snippets.

@pmarkun
Created May 14, 2014 23:43
Show Gist options
  • Select an option

  • Save pmarkun/5cad84e9b87cdac387a6 to your computer and use it in GitHub Desktop.

Select an option

Save pmarkun/5cad84e9b87cdac387a6 to your computer and use it in GitHub Desktop.
import urllib2
from bs4 import BeautifulSoup
header = open("header.html").read() # Arquivo com a cabeca do arquivo
footer = open("footer.html").read() # Arquivo com o pe do arquivo
paginas = urllib2.urlopen("http://www.rtm.net.br/institucional/imprensa/noticias.asp") # Url com a lista de noticias
soup = BeautifulSoup(paginas)
soup = soup.select(".post_cont h3 a") # Busca por links dentro de h3
for x in soup: # Itera por todos os links encontrados
print "Getting " + x.text
pagina = urllib2.urlopen("http://www.rtm.net.br/institucional/imprensa/" + x.get("href")) # Baixa a noticia
conteudo = BeautifulSoup(pagina)
conteudo = conteudo.select(".post_cont")
nome_do_arquivo = x.get("href")[:-3]+'html' # Salva como html, descartando os ultimso 3 digitos originais (asp)
arquivo = open(nome_do_arquivo, "w") # Abre o arquivo
arquivo.write(header + str(conteudo[0]) + footer) # Escreve o conteudo do cabecalho, arquivo, footer
arquivo.close() # Fecha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment