Created
July 11, 2016 12:39
-
-
Save andrefa/f89ed18d73f384e427e69b51159f2756 to your computer and use it in GitHub Desktop.
Lista todas as acoes da bovespa, delay de 15 minutos.
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
from lxml import html | |
import requests | |
separator = '\t' | |
csv = open('stocks.csv', 'w+') | |
csv.write( | |
'NAME' + separator | |
+'DESC' + separator | |
+'PRICE' + separator | |
+'VAR\n') | |
for index in range(1,18): | |
page = requests.get('http://exame.abril.com.br/mercados/cotacoes-bovespa/acoes?page=' + str(index)) | |
tree = html.fromstring(page.content) | |
stocks = tree.xpath('//table[@id="stocks"]/tbody/tr') | |
for row in stocks: | |
data = row.xpath('./td') | |
stockName = data[0].xpath('./a/text()')[0] | |
stockDesc = data[0].xpath('./span/text()')[0] | |
stockPrice = data[1].text_content().replace('.','').replace(',','.') | |
stockDelta = data[2].text_content().replace('.','').replace(',','.') | |
csv.write( | |
stockName + separator | |
+stockDesc + separator | |
+stockPrice + separator | |
+stockDelta + '\n' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment