Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from lxml import etree
- import urllib
- import sqlite3
- def url2db(db_name,st_id,st_name):
- ''' INSERT DATA INTO DB '''
- try:
- conn = sqlite3.connect(db_name)
- c = conn.cursor()
- c.execute("INSERT OR FAIL INTO stations VALUES (?,?)", (st_id,st_name))
- print 'a new data added to database!'
- conn.commit()
- conn.close()
- except:
- print "INSERT error!"
- web = urllib.urlopen("http://meteocenter.net/_world_weather_stations.htm")
- s = web.read()
- html = etree.HTML(s)
- ## Get all 'tr'
- tr_nodes = html.xpath('//table/tr')
- ## 'th' is inside first 'tr'
- header = [i[0].text for i in tr_nodes[0].xpath("th")]
- ## Get text from rest all 'tr'
- td_content = [[td.text for td in tr.xpath('td')] for tr in tr_nodes[1:]]
- for istation in td_content:
- if (istation[-1]==u"Российская Федерация"):
- print istation[0], istation[1]
- url2db('/home/www-data/db/meteo',istation[0], istation[1])
Advertisement
Add Comment
Please, Sign In to add comment