猫哥教你写爬虫 040--存储数据-做业

小做业:爬取猫眼电影top100, 保存 电影名, 排名, 评分, 主演, 封面图片, 上映时间到数据库

maoyan.com/board/4

1559375538602

代码

import requests, pymysql
from bs4 import BeautifulSoup
# 链接数据库
connection = pymysql.connect(host='localhost', user='root', password='root', port=3306)
# 建立游标
cursor = connection.cursor()
# 建库, 建表
def create_table():
    # 若是存在即删除数据库 maoyan
    cursor.execute('drop database if exists maoyan')
    # 建立maoyan数据库, 字符集utf8
    cursor.execute('create database maoyan character set utf8')
    # 使用数据库
    cursor.execute('use maoyan')
    # 建立数据表movie, id主键, title电影名, score评分, img图片路径, actors主演, release_date上映日期, ranking排名
    cursor.execute(''' create table movie( id int primary key auto_increment, title varchar(255) not null, score decimal(2,1) not null, img varchar(255) not null, actors varchar(255) not null, release_date varchar(20) not null, ranking int not null ) ''')
    # 提交sql语句, 增删改须要提交
    connection.commit()
# 获取电影信息
def get_movies():
    # https://maoyan.com/board/4?offset=0
    # 0,1,2,3,4,5,6,7,8,9
    for i in range(1):
        print(i)
        soup = BeautifulSoup(requests.get('https://maoyan.com/board/4?offset={}'.format(i*10)).text, 'html.parser')
        # 在dl里面找dd, 每一个dd都保存了一部电影的信息
        for dd in soup.find('dl', class_='board-wrapper').find_all('dd'):
            # 电影标题
            title = dd.find('a')['title']
            # 电影的图片连接
            img = dd.find('img', class_='board-img')['data-src'].split('jpg')[0]+'jpg'
            # 电影的主演
            actors = dd.find('p', class_='star').text.strip()
            # 电影的评分
            score = dd.find('p', class_='score').text
            # 电影的排行
            ranking = dd.find('i').text
            # 电影的上映日期
            release_date = dd.find('p',class_="releasetime").text[5:15]
            # 插入数据库
            add_movies(title, img, actors, score, ranking, release_date)
# 把电影信息写入数据库
def add_movies(title, img, actors, score, ranking, release_date):
    # 拼接sql语句
    sql = "insert into movie(title, img, actors, score, ranking, release_date) values({},{},{},{},{},{})".format(
        repr(title), repr(img), repr(actors), score, ranking, repr(release_date))
    print(sql)
    # 执行
    cursor.execute(sql)
    # 提交
    connection.commit()
def query_movie():
    sql = "select * from movie"
    # 执行sql
    cursor.execute(sql)
    # 获取全部数据
    res = cursor.fetchall()
    for i in res:
        print(i)
if __name__ == "__main__":
    create_table()
    get_movies()
    query_movie()
复制代码

快速跳转:

猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--做业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小做业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小做业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同窗.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小做业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小做业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小做业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-做业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-做业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-做业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小做业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小做业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-做业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-做业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-做业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-做业-电影top250-做业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-做业.md
猫哥教你写爬虫 041--模拟登陆-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-做业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.mdhtml

相关文章
相关标签/搜索