怎么学网站设计,优秀vi设计网站,做网站的是如何赚钱的,销售平台是什么意思之前的文章有关于更多操作方式详细解答#xff0c;本篇基于前面的知识点进行操作#xff0c;如果不了解可以先看之前的文章 Python爬虫#xff08;8#xff09;selenium爬虫后数据#xff0c;存入sqlit3实现增删改查导入默认包和环境元素定位创建一个sqlit3表将爬虫到的信… 之前的文章有关于更多操作方式详细解答本篇基于前面的知识点进行操作如果不了解可以先看之前的文章
Python爬虫8selenium爬虫后数据存入sqlit3实现增删改查导入默认包和环境元素定位创建一个sqlit3表将爬虫到的信息插入表中在if __name__ __main__:中调用def的名称即可如删除表中信息如果我们需要删除指定表中数据可以这样做修改表中信息查询表中信息本篇主要是以房地产的数据为主主要就是要用爬虫爬取一个标题和房型、面积、具体地址、小区名这几个标签为主然后将这些数据存入sqlit3数据库中
导入默认包和环境
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
import sqlite3
opt ChromeOptions() # 创建Chrome参数对象
opt.headless True # 把Chrome设置成可视化无界面模式windows/Linux 皆可
driver Chrome(optionsopt) # 创建Chrome无界面对象
driver.get(房地产网站)元素定位
由于只是进行单一的元素存储所以这里就直接用固定的xpath的定位
def sc():housename driver.find_element(By.XPATH, //*[idcontainer]/div[2]/div[1]/div[3]/div[1]/div/a[1]/span).texthouseaddress driver.find_element(By.XPATH,//*[idcontainer]/div[2]/div[1]/div[3]/div[1]/div/a[2]/span).texthousearea driver.find_element(By.XPATH, //*[idcontainer]/div[2]/div[1]/div[3]/div[1]/div/a[3]).textprint(housename,houseaddress,housearea)创建一个sqlit3表
sql create table house(housename_s varchar(100), \houseaddress_s varchar(100),housearea_s varchar(100))try:cur.execute(sql)#将数据插入数据库con.commit() #提交事物print(插入成功)except Exception as e:print(e)print(插入失败)con.rollback() #回滚事物finally:#关闭游标cur.close()#关闭连接con.close()将爬虫到的信息插入表中
#插入表
def into(ent):#连接sqlit3表con sqlite3.connect(house.db)# 获取cursor对象cur con.cursor()#将爬虫到的数据插入表sql insert into house(housename_s,houseaddress_s,housearea_s) values(?,?,?)try:cur.execute(sql,ent)#将数据插入数据库con.commit() #提交事物print(插入成功)except Exception as e:print(e)print(插入失败)con.rollback() #回滚事物finally:#关闭游标cur.close()#关闭连接con.close()在if name “main”:中调用def的名称即可如
sc()
删除表中信息
删除表 drop table table_name 判断表中是否存在信息 drop table if exists table_name 删除表内所有数据 DELETE FROM house;
#删除表中数据
def delete():#连接sqlit3表con sqlite3.connect(house.db)delcon con.cursor()try:delcon.execute(DROP table if exists house)con.commit()print(表删除成功)except Exception as e:print(表删除失败)如果我们需要删除指定表中数据可以这样做
def delete():#连接sqlit3表con sqlite3.connect(house1.db)delcon con.cursor()try:delcon.execute(DELETE FROM house WHERE houseaddress_s 1;)con.commit()print(表内数据删除成功)except Exception as e:print(表内数据删除失败)修改表中信息
def update():#连接sqlit3表con sqlite3.connect(house1.db)delcon con.cursor()try:delcon.execute(UPDATE house SET housename_s 万科 where housename_s 1;)con.commit()print(表内数据更新成功)except Exception as e:print(表内数据更新失败)查询表中信息
def update():#连接sqlit3表con sqlite3.connect(house1.db)delcon con.cursor()delcon.execute(select * from house ;)print(delcon.fetchone())#捕获查询