当前位置: 首页 > news >正文

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

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

Python爬虫(8)selenium爬虫后数据,存入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(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').texthouseaddress = driver.find_element(By.XPATH,'//*[@id="container"]/div[2]/div[1]/div[3]/div[1]/div/a[2]/span').texthousearea = driver.find_element(By.XPATH, '//*[@id="container"]/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())#捕获查询

在这里插入图片描述

http://www.lryc.cn/news/14643.html

相关文章:

  • 最全Linux驱动开发全流程详细解析(持续更新)
  • 华为OD机试 - 乱序整数序列两数之和绝对值最小 | 机试题算法思路 【2023】
  • 网上插画教学哪家质量好,汇总5大插画培训班
  • 对云原生集群网络流量可观测性的一点思考
  • 借力英特尔® Smart Edge,灵雀云 ACP 5G 专网解决方案获得多维度优化加速
  • 【Pytorch项目实战】基于PaddlenHub的口罩检测与语音提示
  • EasyExcel 让Excel导入导出更简单
  • 华为OD机试 - 需要广播的服务器数量 | 机试题算法思路 【2023】
  • 三次握手四次挥手详细解析面试常问
  • 组合由于继承
  • 大学计算机基础 知识点总结
  • 手撸React组件库前必须清楚的9个问题
  • 试用国内及国外AI绘图软件后的总结
  • DJI 无人机 Onboard SDK ROS 功能包demo运行
  • 揭开JavaWeb中Cookie与Session的神秘面纱
  • 2023-02-20 Qt 5.13.1 + OpenCV 4.5.4环境编译
  • 波次分拣系统
  • 【Servlet篇】Request请求转发详细解读
  • vector
  • LeetCode——104. 二叉树的最大深度
  • 漫画 | Python是一门烂语言?
  • 2023.2 新方案 java代码混淆 java加密 字符串加密
  • Swift 周报 第二十三期
  • android系统屏幕旋转角度,应用界面横竖屏,设备旋转角度,三者的区别以及使用。
  • 【华为云-开发者专属集市】DevCloud+ECS、MySQL搭建WordPress
  • Milvus 群星闪耀时|又一个小目标达成 :社区正式突破 15,000 星!
  • Qt信号与槽使用方法总结
  • SpringCloud alibaba-Sentinel服务降级策略
  • python常用函数——random()函数
  • PX4之启动脚本