python 將爬取的資料儲存在資料庫裡
阿新 • • 發佈:2018-11-23
python 將爬取的資料儲存在資料庫裡
import urllib.request import re import sqlite3 response = urllib.request.urlopen("https://search.51job.com/list/010000%252C020000%252C030200%252C040000,000000,0000,00,9,99,python,2,1.html") html = response.read() html = html.decode("GBK") lst = re. findall('<span class="t3">(北京|上海|廣州|深圳).*</span>\s*<span class="t4">(\d*\.?\d*)-(\d*\.?\d*)(\w)/(.*)</span>',html) conn = sqlite3.connect(r'D:\\2.db') cursor = conn.cursor() cursor.execute('create table IF NOT EXISTS result51job(id integer primary key,key varchar(20),addr varchar(40),momin float,momax float)') for i in lst: min = float(i[1]) max = float(i[2]) if i[3] == "千": min /= 10 max /= 10 if i[4] == "年": min /= 12 max /= 12 cursor.execute("insert into result51job(key,addr,momin,momax) values ('%s','%s','%f','%f')" % ("Python",i[0],min,max)) conn.commit(); cursor.execute("select * from result51job") values=cursor.fetchall() print(values) cursor.close() conn.close()