分分鐘教你爬數據

得到 html

import requests
html=requests.get('http://sc.hkex.com.hk/TuniS/www.hkex.com.hk/chi/market/sec_tradinfo/stockcode/eisdeqty_c.htm').content

解析數據

from pyquery import PyQuery as Q
q=Q(html)
tr = q('tr.tr_normal')

導入 db

db=zpool['mysql+mysqldb://root:pwd@dbhost:3306/glhdb']
sqls = ["INSERT INTO `stocks_code` (`name`, `code`) VALUES ('{0}','{1}')".format(Q(i)('td')[0].text.encode('utf8','ignore'), ((Q(Q(i)('td')[1])('a') and Q(Q(i)('td')[1])('a')[0].text) or u'').encode('utf8','ignore').strip(')').strip('\'').replace('\'',"\\'")) for i in tr[0:-3]]
[db.execute(text(i)) for i in sqls]
發佈了96 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章