import urllib.request
import re
import sqlite3
def searchWeb(html):
rows=[]
def saveDB(rows):
if len(rows) == 0:
return
try:
con = sqlite3.connect("students.db")
cursor = con.cursor()
try:
cursor.execute("drop table students")
except:
pass
try:
sql = "create table students(No varchar(128)primary key,Name varchar(128),Gender varchar(128),Age int)"
cursor.execute(sql)
except:
pass
for row in rows:
if (len(row) == 4):
sql = "insert into students(No,Name,Gender,Age)values(?,?,?,?)"
try:
No = row[0]
Name = row[1]
Gender = row[2]
Age = int(row[3])
cursor.execute(sql, (No, Name, Gender, Age))
except Exception as err:
print(err)
con.commit()
con.close()
except Exception as err:
print(err);
def showWeb(rows):
print("Showing data from Web...")
for row in rows:
print(row)
def showDB():
print("Showing data from DB...")
try:
con=sqlite3.connect("students.db")
cursor=con.cursor()
cursor.execute("select*from students")
rows=cursor.fetchall()
for row in rows:
print(row)
con.close()
except Exception as err:
print(err)
try:
url="http://127.0.0.1:5000"
resp=urllib.request.urlopen(url)
data=https://bbs.csdn.net/topics/resp.read()
html=data.decode("utf-8")
rows=searchWeb(html)
showWeb(rows)
saveDB(rows)
showDB()
except Exception as e:
print(e)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/278623.html
標籤:其他開發語言
