Last active
February 20, 2023 19:02
-
-
Save reulison/e46826039fda210e05069b38c1f44368 to your computer and use it in GitHub Desktop.
Web Scraping com Python e BeautifulSoup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
from bs4 import BeautifulSoup | |
wiki = 'https://pt.wikipedia.org/wiki/Lista_de_capitais_do_Brasil_por_%C3%A1rea' | |
page = urllib.request.urlopen(wiki) | |
soup = BeautifulSoup(page, 'html.parser') | |
table = soup.find('table') | |
A=[] | |
B=[] | |
C=[] | |
D=[] | |
E=[] | |
for row in table.findAll('tr'): | |
cells = row.findAll('td') | |
if len(cells)==5: | |
A.append(cells[0].find(text=True)) | |
B.append(cells[1].find(text=True)) | |
C.append(cells[2].find(text=True)) | |
D.append(cells[3].find('a').text) | |
E.append(cells[4].find(text=True)) | |
import pandas as pd | |
df = pd.DataFrame(index=A, columns=['Posição']) | |
df['Posição']=A | |
df['Estado']=B | |
df['Código/IBGE']=C | |
df['Capital']=D | |
df['Área']=E | |
df |
Muito legal este artigo, grato por compartilhar, apenas algumas observações, acredito que "Capital" e "Estado" estejam com posições invertidas, rodando o código aqui percebi que não aparecem os nomes dos estados Bahia e Pará :-)
Mais uma vez parabéns pelo artigo !!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Existe um artigo explicando os detalhes. Acesse aqui!