Python爬虫(8)selenium爬虫后数据,存入sqlit3实现增删改查

在这里插入图片描述
之前的文章有关于更多操作方式详细解答,本篇基于前面的知识点进行操作,如果不了解可以先看之前的文章

本篇主要是以房地产的数据为主,主要就是要用爬虫爬取一个标题和房型、面积、具体地址、小区名这几个标签为主,然后将这些数据存入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(options=opt)     # 创建Chrome无界面对象
driver.get("房地产网站")

元素定位

由于只是进行单一的元素存储所以这里就直接用固定的xpath的定位

def sc():
    housename = driver.find_element(By.XPATH, '//*[@id="container"]/div[2]/div[1]/div[3]/div[1]/div/a[1]/span').text
    houseaddress = driver.find_element(By.XPATH,'//*[@id="container"]/div[2]/div[1]/div[3]/div[1]/div/a[2]/span').text
    housearea = driver.find_element(By.XPATH, '//*[@id="container"]/div[2]/div[1]/div[3]/div[1]/div/a[3]').text
    print(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())#捕获查询

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻烟飘荡

我想吃甜筒

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值