Python 爬取数据入库mysql
时间:2021-07-01 10:21:17
帮助过:24人阅读
# -*- enconding:etf-8 -*-
2 import pymysql
3 import os
4 import time
5 import re
6 serveraddr=
"localhost"
7 user=
"root"
8 password=
"123456"
9 databaseName=
"test"
10 filename=
"./data/UNCL.csv"
11
12 def create_table():
13 # sep=os.sep
14 # tableName=raw_input(‘please input the table name which will be created:‘)
15 # classpath=raw_input(‘请输入需要遍历的路径:‘)
16
17 db=
pymysql.connect(serveraddr,user,password,databaseName)
18 cursor=
db.cursor()
19 cursor.execute(
"drop table if exists `tncl`")
20 sql=
""" create table `tncl`(
21 `tncl_id` varchar(25) not null,
22 `tncl_tag` varchar(25) not null,
23 `tncl_desc` varchar(255) not null,
24 `tncl_note` varchar(1200) not null,
25 primary key(`tncl_id`)
26 ) engine=InnoDB default charset=utf8;"""
27
28 cursor.execute(sql)
29 db.close()
30 def test():
31 p1=r
"^\s{13}\w.+|\n$"
32 pattern=
re.compile(p1)
33 fr=
open(filename)
34 w2=open(
‘./data/e.csv‘,
‘a‘)
35 for line
in fr.readlines():
36 # print(line)
37 matcher=
re.findall(pattern,line)
38 # print(matcher)
39 # print(type(matcher))
40 for i
in matcher:
41 w2.write(i)
42 # w2.write("\n")
43 fr.close()
44 w2.close()
45
46
47 if __name__==
‘__main__‘:
48 test()
Python 爬取数据入库mysql
标签:.exe cursor code print input findall mat root 取数据