将爬虫数据写入数据库

复制代码
1. 在Linux机器上安装和配置好数据库
    安装:yum install mariadb-server -y
        mysql
    配置:防火墙(启用) -> 规则 (允许/禁止ip,port,协议) -> 关掉
         阿里云/腾讯云 -> 安全组
         应用授权 -> python(主机)连接 mysql(主机/用户名/密码)

2.

sys => root => 123456
mysql -uroot -p => db123456
权限远程访问 => root/123456
在xshell端命令
 mysql -uroot -p
show databases;
use mysql;
GRANT ALL PRIVILEGES ON *.* to 'root'@'%' IDENTIFIED BY "123456";
 FLUSH PRIVILEGES;
SELECT user,host FROM user;

python操作数据库的模块
      PyMySQL
      SQLALCHEMY(更高级->支持MySQL及其他数据,支持MySQL依赖于PyMySQL)
         ORM => 对象关系映射
在pycharm打开数据库进行连接 然后再此进行操作
"""

# pip install Pymysql

要现在虚拟机连接数据库并创建 create database pachong;

演示从豆瓣网爬取电影信息 并保存到MySQL中

python 复制代码
import pymysql
import json
import download
# 创建数据库连接
db = pymysql.connect(host="192.168.159.142",
                     port=3306,
                     user="root",
                     password="123456",
                     db="pachong",
                     charset='utf8mb4')
# 创建游标(执行)
cursor = db.cursor()
# 执行sql语句:execute(sql)
sql = "DROP TABLE IF EXISTS movies"
cursor.execute(sql)
sql = """CREATE TABLE movies( 
        title CHAR(50) CHARACTER SET utf8mb4 NOT NULL ,
        rate CHAR(20),
        cover CHAR(200));
      """
cursor.execute(sql)
# name = ["热门","最新","豆瓣高分","冷门佳片","华语","欧美","韩国","日本"]DECIMAL(3, 1)
name = ["热门"]
for i in name:
    all_movies = download.get_text(f"https://movie.douban.com/j/search_subjects?type=movie&tag={i}&page_limit=50&page_start=0")
    all_movies_obj = json.loads(all_movies)
    for movie in all_movies_obj["subjects"]:
        sql = "INSERT INTO movies (title, rate, cover) VALUES (%s, %s, %s)"
        data = (movie['title'], movie['rate'], movie['cover'])
        cursor.execute(sql, data)
db.commit()
python 复制代码
# # 增删改查
# sql = "INSERT INTO app(id,name) VALUES(1,'嘻嘻嘻');"
# cursor.execute(sql)
# sql = "INSERT INTO app(id,name) VALUES (%s,%s)"
# datas = [(2,'咯咯咯'),(3,'嗯嗯嗯'),(4,'哈哈哈')]
# cursor.executemany(sql,datas)
# print(1)
#
# # 查询
# sql = "SELECT * FROM app;"
# cursor.execute(sql)
# result = cursor.execute(sql)
# print(result)
# result = cursor.fetchone()
# print(result)
# result = cursor.fetchmany(2)
# print(result)
# result = cursor.fetchall()
# print(result)
# # 更新
# sql = "UPDATE app SET name='李琳' WHERE id=1"
# cursor.execute(sql)
#
# sql = "DELETE FROM app WHERE name like '嘻%'"
# cursor.execute(sql)
# result = cursor.execute(sql)
# print(result)
# db.commit()# 提交修改到数据库
相关推荐
明月清风徐徐14 分钟前
Scrapy爬取豆瓣电影Top250排行榜
python·selenium·scrapy
theLuckyLong15 分钟前
SpringBoot后端解决跨域问题
spring boot·后端·python
Yongqiang Cheng18 分钟前
Python operator.itemgetter(item) and operator.itemgetter(*items)
python·operator·itemgetter
MavenTalk21 分钟前
Move开发语言在区块链的开发与应用
开发语言·python·rust·区块链·solidity·move
FksLiao33 分钟前
Superset安装
python
L Jiawen40 分钟前
【Python · PyTorch】卷积神经网络(基础概念)
pytorch·python·cnn
goomind1 小时前
深度学习模型评价指标介绍
人工智能·python·深度学习·计算机视觉
日里安1 小时前
8. 基于 Redis 实现限流
数据库·redis·缓存
->yjy1 小时前
wordcloud库基本介绍
python
2401_840192271 小时前
python基础大杂烩
linux·开发语言·python