Olá pessoal.
Quando deixo o código rodando até a linha 11, consigo imprimir a lista de links normalmente, mas não sei como inserir apenas os links de extensão.pdf.
Quando rodo o código todo, obtenho o erro descrito abaixo.
Alguém poderia me ajudar?
import requests
import httplib2
import os
from bs4 import BeautifulSoup, SoupStrainer
artigos = []
http = httplib2.Http()
for link in BeautifulSoup(response, parse_only=SoupStrainer('a')):
if link.has_attr('href'):
artigos.append(link['href'])
print(artigos)
def baixa_arquivo(url, endereco):
resposta = requests.get(url)
if resposta.status_code == requests.codes.OK:
with open(endereco, 'wb') as novo_arquivo:
novo_arquivo.write(resposta.content)
print('Download concluído. Salvo em {}'.format(endereco))
else:
resposta.raise_for_status()
if __name__ == '__main__':
output = 'Download'
for i in range(0, len(artigos) + 1):
nome_do_arquivo = os.path.join(output, 'artigo{}.pdf'.format(i))
a = artigos[i]
baixa_arquivo(url_basica.format(a), nome_do_arquivo)
ERRO:
C:\Downloads\webscraping\venv\Scripts\python.exe C:/Downloads/webscraping/requisicoes.py
C:\Downloads\webscraping\requisicoes.py:9: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
The code that caused this warning is on line 9 of the file C:\Downloads\webscraping\requisicoes.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.
for link in BeautifulSoup(response, parse_only=SoupStrainer('a')):
['site/capa']
Traceback (most recent call last):
File "C:\Downloads\webscraping\requisicoes.py", line 26, in <module>
a = artigos[i]
IndexError: list index out of range
Download concluído. Salvo em Download\artigo0.pdf
Process finished with exit code 1