手把手带你入门Python爬虫(三、PyMySQL)
2023-09-11 14:19:17 时间
PyMySQL
1、安装
pip install PyMySQL
2、新建Mysql数据表
使用Navcat for Mysql
创建数据库py_spider
,创建数据表users
,新建字段:
3、使用pymysql
import pymysql.cursors
# 连接数据库
connection = pymysql.connect(host='localhost',
port=3307,
user='root',
password='root',
db='py_spider',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
# 插入一条数据
with connection.cursor() as cursor:
sql = "insert into `users` (`email`,`password`) values (%s, %s)"
cursor.execute(sql, ('zhangsan@qq.com','123456'))
connection.commit()
# 查询数据
with connection.cursor() as cursor:
sql = "select * from `users` where `email`=%s"
cursor.execute(sql, ('zhangsan@qq.com',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
相关文章
- Python爬虫之selenium高级功能
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(xe4xb8xb0xe5xa)的解决方法
- 第三百七十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapyd部署scrapy项目
- 第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门搜索
- 小白学 Python 爬虫(40):爬虫框架 Scrapy 入门基础(七)对接 Selenium 实战
- 小白学 Python 爬虫(39): JavaScript 渲染服务 scrapy-splash 入门
- 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
- 小白学 Python 爬虫(33):爬虫框架 Scrapy 入门基础(一)
- 小白学 Python 爬虫(23):解析库 pyquery 入门
- 小白学 Python 爬虫(4):前置准备(三)Docker基础入门
- 小白学 Python 爬虫(3):前置准备(二)Linux基础入门
- Python 爬虫入门实战
- 小白如何入门 Python 爬虫?
- Python爬虫实战,Request+urllib模块,批量下载爬取飙歌榜所有音乐文件
- Python从入门到爬虫案例实现~
- Python爬虫入门BeautifulSoup模块
- Python爬虫:xpath常用方法示例
- Python爬虫:browsercookie库获取浏览器cookie
- 爬虫日记(2):为什么用Python来开发爬虫
- python基础===jieba模块,Python 中文分词组件
- 学习python 115小时后,告诉想学爬虫的你,别怕,爬虫,没那么难抓!
- 10个Python爬虫入门实例
- Python编程语言学习:python中浅复制/深复制(或浅拷贝/深拷贝)的简介、案例应用注意事项之详细攻略
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- 从零开始,学会Python爬虫不再难!!! -- (5)截流:从网络包中获取数据 丨蓄力计划
- Python常用内置函数(python 3.x)