Created
June 10, 2010 05:58
-
-
Save ccarpenterg/432607 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
''' | |
Autor: Cristian Carpenter G. | |
''' | |
from BeautifulSoup import BeautifulSoup | |
import urllib, re | |
import MySQLdb | |
from datetime import datetime | |
def number(string): | |
foo = string.replace('.', '') | |
foo = foo.replace(',', '.') | |
foo = foo.replace(' ', '') | |
return foo | |
def clean(string): | |
foo = string.replace('.', '') | |
foo = foo.replace (' ', '') | |
return foo | |
db = MySQLdb.Connect(user="root", passwd="******", db="ipsa") | |
stockdata = db.cursor() | |
html = urllib.urlopen("http://www.bolsadesantiago.com/web/bcs/preciosacciones").read() | |
soup = BeautifulSoup(html) | |
prices = soup.findAll(attrs={'class': re.compile("bcs-portlet-section")}) | |
for i in range(1, 41): | |
symbol = prices[i].findAll(attrs={"class": "col-1"})[0]('a')[0].find(text=True) | |
price = prices[i].findAll(attrs={"class": "col-2"})[0].find(text=True) | |
volume = prices[i].findAll(attrs={"class": "col-4"})[0].find(text=True) | |
price = number(price) | |
volume = clean(volume) | |
date = datetime.today() | |
date = date.date() | |
stockdata.execute("""INSERT INTO acciones (symbol, price, volume, date) VALUES (%s, %s, %s, %s)""", (symbol, price, volume, date)) | |
db.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment