Last active
August 29, 2015 14:04
-
-
Save msanatan/9d0eee0ce907fdd651e6 to your computer and use it in GitHub Desktop.
Scraper for wholesale fish market prices in Trinidad and Tobago
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 | |
import time | |
date = time.strftime('%d-%m-%Y') | |
output = '' | |
try: | |
page = requests.get('http://www.namistt.com/') | |
except requests.exceptions.RequestException as e: | |
print 'Could not reach the website:' | |
raise e | |
tree = html.fromstring(page.text) | |
price_tables = tree.get_element_by_id('tbl') | |
print 'Retrieving table data' | |
for row in price_tables.findall('.//tr'): | |
row = [r.xpath('./text()')[0].strip() for r in row] | |
if row[2]: | |
print row | |
output += ','.join(row) + '\n' | |
f = open('NAMIS Wholesale Fish Market Prices {0}.csv'.format(date), 'w') | |
print 'Writing to file' | |
f.write(output) | |
f.close() | |
print 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment