一,连接数据库
1.连接MySQL
import pymysql
conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='***',db='***', charset='utf8')
cur = conn.cursor()
# 以下两步把游标与数据库连接都关闭,这也是必须的!
cur.close()
conn.close()
往MySQL中插入数据
try:
cur.execute("USE ssm_crud")
cur.execute('SET NAMES utf8')
cur.execute('SET CHARACTER SET utf8')
cur.execute('SET character_set_connection=utf8')
cur.execute(
'insert into GYZC_XCUL(索引号,信息分类,发布机构,发文日期,文号,是否有效,信息名称) values(%s,%s,%s,%s,%s,%s,%s)',
[list[0], list[1], list[2], list[3], list[4], list[5], list[6]]
)
except Exception as e:
print(e)
finally:
pass
2.连接Oracle
import cx_Oracle #oralce连接工具
conn = cx_Oracle.connect('用户名', '密码', 'localhost:1521/服务器名(service_name)') # 连接数据库
cur = conn.cursor() # 获取cursor
cur.close()
conn.close()
往Oracle中插入数据,并且转换数据类型
try:
sql = "insert into swzc(uuid,xmbh,bdmc,gpjg,gpqsrq,gpjzrq,gpggq,bmjzsj,fbmtmc,bdszd,wqlb)values( '"+str(ncount)+"' ,'" +nlist_td[1].encode('GBK', 'ignore').decode('GBk') + "','" + nlist_td[0].encode('GBK', 'ignore').decode('GBk') + "','" + nlist_td[2].encode('GBK', 'ignore').decode('GBk') + "',to_date('"+nlist_td[3]+"','yyyy/mm/dd HH:MI:SS'),to_date('"+nlist_td[4]+"','yyyy/mm/dd HH:MI:SS'),'" + nlist_td[5].encode('GBK', 'ignore').decode('GBk') + "','" + nlist_td[6].encode('GBK', 'ignore').decode('GBk') + "','" + nlist_td[7].encode('GBK', 'ignore').decode('GBk') + "','" + nlist_td[8].encode('GBK', 'ignore').decode('GBk') + "','" +nlist_td[9].encode('GBK', 'ignore').decode('GBk')+ "')"
test="to_date('"+nlist_td[3]+"','yyyy/mm/dd HH:MI:SS')"
print(test)
cur.execute(sql)
except Exception as e:
print(e)
finally:
pass
二,获取网页内容
1.使用urllib库中的request包
from urllib.request import urlopen
import urllib.request
headers = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
opener = urllib.request