时间:2021-07-01 10:21:17 帮助过:16人阅读
6.数据库插入:
import cx_Oracle
conn = cx_Oracle.connect(‘xzp/xzp@localhost/testdb‘)
cursor = conn.cursor()
cursor.execute ("CREATE TABLE INSERTTEST(ID INT, C1 VARCHAR(50), C2 VARCHAR(50), C3 VARCHAR(50))")
cursor.execute ("INSERT INTO INSERTTEST (ID, COL1, COL2, COL3)VALUES(1213412, ‘asdfa‘, ‘ewewe‘, ‘sfjgsfg‘)")
cursor.execute ("INSERT INTO INSERTTEST (ID, COL1, COL2, COL3)VALUES(12341, ‘ashdfh‘, ‘shhsdfh‘, ‘sghs‘)")
cursor.execute ("INSERT INTO INSERTTEST (ID, COL1, COL2, COL3)VALUES(123451235, ‘werwerw‘, ‘asdfaf‘, ‘awew‘)")
conn.commit() #这里一定要commit才行,要不然数据是不会插入的
cursor.close()
conn.close()
7案例:从某网站上面爬取彩票号码
import re
import urllib
import cx_Oracle
import urllib.request
def getHtml(url):
page = urllib.request.urlopen(url)
html= page.read()
return html
def getNumber(html):
reg = r‘<li class="ball_red">(\d{2})</li>‘
reg2 = r‘<li class="ball_blue">(\d{2})</li>‘
regqnumber = r‘第 <font class="cfont2"><strong>(\d*)</strong></font>‘
number = re.compile(reg)
numberblue = re.compile(reg2)
qnumber = re.compile(regqnumber)
numberlist = re.findall(number,html.decode(‘gbk‘))
numberblue = re.findall(numberblue,html.decode(‘gbk‘))
qnum = re.findall(qnumber,html.decode(‘gbk‘))
for number in numberblue:
numberlist.append(number)
for n in qnum:
numberlist.append(n)
print(numberlist)
return numberlist
#将查询到的号码入库
def RecodeToOracle(list):
conn = cx_Oracle.connect(‘xzp/xzp@localhost/testdb.domain‘)
cur = conn.cursor()
sql = "INSERT INTO SSQ (REDNUM1,REDNUM2,REDNUM3,REDNUM4,REDNUM5,REDNUM6,BLUENUM,QNUMBER) VALUES (‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘)"%(list[0],list[1],list[2],list[3],list[4],list[5],list[6],list[7])
cur.execute(sql)
cur.close()
conn.commit()#这里一定要提交,要不然是没有办法将数据入库的
conn.close()#记得要关闭会话
def Geturl(html):
reg = r‘上一期:<a href="(.*)" target="_blank">‘
url = re.compile(reg)
urllist = re.findall(url,html.decode(‘gbk‘))
if len(urllist)!=0:
print(urllist[0])
if urllist[0].index(‘http:‘)<0:
urllist[0]=‘http:‘+urllist[0]
htmlbefore = getHtml(urllist[0])
# print(htmlbefore)
numberlist = getNumber(htmlbefore)
print(len(numberlist))
RecodeToOracle(numberlist)
print(numberlist)
Geturl(htmlbefore)
else:
return
str1 = ‘网站地址‘
html1 = getHtml(str1)
RecodeToOracle(getNumber(html1))
Geturl(html1)
---------------------
作者:浅颜半夏
来源:CSDN
原文:https://blog.csdn.net/xiazhipeng1000/article/details/78987036
版权声明:本文为博主原创文章,转载请附上博文链接!
python 操作Oracle数据库
标签:python36 数据库名 test acl geturl 数据库连接 cci com 参考