Created
September 17, 2011 18:02
-
-
Save netdesign/1224184 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # -*- coding: utf-8 -*- | |
| import lxml.html as jq | |
| import uuid | |
| import hashlib | |
| import oursql | |
| db = oursql.connect(host="127.0.0.1", user="user", passwd="pwd", db="mydata", charset='utf8') | |
| query = db.cursor(plain_query=True) | |
| file = open('/path/to/bookmarks.html', 'r') | |
| content = file.read() | |
| mydata = jq.document_fromstring(content) | |
| query.execute("SHOW VARIABLES LIKE 'character_set%';") | |
| print query.fetchone() | |
| links = {} | |
| for value in mydata.xpath("//dl"): | |
| for link in value.xpath("//a"): | |
| try: | |
| uid = str(uuid.uuid1()) | |
| sha = hashlib.sha1() | |
| sha.update(uid) | |
| myUid = sha.hexdigest() | |
| query.execute("set names utf8") | |
| query.execute("INSERT INTO bookmarks (uid, url, title) VALUES(?, ?, ?)", (myUid, link.attrib['href'].decode('utf8'), link.text_content().decode('utf8'))) | |
| except: | |
| pass | |
| print "Error" | |
| print link.attrib['href']+" "+(link.text_content().decode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment