将爬虫数据写入数据库

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中

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()
# # 增删改查
# 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()# 提交修改到数据库

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值