爬取猫眼电影top100,request、beautifulsoup运用

这是第三篇爬虫实战,运用request请求,beautifulsoup解析,mysql储存。
如果你正在学习爬虫,本文是比较好的选择,建议在学习的时候打开猫眼电影top100进行标签的选择,具体分析步骤就省略啦,具体的方法可看我以前的总结。

练习猫眼top100是为了复习request和beautifulsoup以及sql中表的创建、字段类型的设置。好了附上源代码

import requests
from bs4 import BeautifulSoup
from requests.exceptions import RequestException

headers = {
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"}

def get_page_index(url):
    try:
        respones = requests.get(url, headers=headers)  # get请求
        if respones.status_code == 200:
            return respones.text
        return None
    except RequestException:
        print("请求有错误")
        return None

def parse_one_page(html):
    soup = BeautifulSoup(html, 'lxml')
    board_wrapper = soup.find_all('dl', class_='board-wrapper')[0]
    board_index = board_wrapper.find_all('dd')
    for link in board_index:
        title = link.find_all('p', class_='name')[0].text
        board_index = link.find_all('i')[0].text
        star = link.find_all('p', class_='star')[0].text.strip()[3:]
        releasetime = link.find_all('p', class_='releasetime')[0].text.strip()[5:]
        score = link.find_all('p', class_='score')[0].text
        print(board_index, title, star, releasetime, score) 

def main():
    t = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
    urls = ['https://maoyan.com/board/4?offset={}'.format(i) for i in t]
    for url in urls:
        html = get_page_index(url)
        parse_one_page(html)

if __name__ == "__main__":
    main()

爬取内容如下:

存入数据库

存入数据库01:新建表
ALTER TABLE maoyantop100 CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
create table maoyantop100
(board_index int not null,
title varchar(20) not null,
star varchar(50) not null,
releasetime varchar(50) not null,
score float not null);
存入数据库02
import pymysql
import requests
import json
from bs4 import BeautifulSoup
from requests.exceptions import RequestException

headers = {
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"}

def get_page_index(url):
    try:
        respones = requests.get(url, headers=headers)  # get请求
        if respones.status_code == 200:
            return respones.text
        return None
    except RequestException:
        print("请求有错误")
        return None

def parse_one_page(html):
    soup = BeautifulSoup(html, 'lxml')
    board_wrapper = soup.find_all('dl', class_='board-wrapper')[0]
    board_index = board_wrapper.find_all('dd')
    for link in board_index:
        title = link.find_all('p', class_='name')[0].text
        board_index = link.find_all('i')[0].text
        star = link.find_all('p', class_='star')[0].text.strip()[3:]
        releasetime = link.find_all('p', class_='releasetime')[0].text.strip()[5:]
        score = link.find_all('p', class_='score')[0].text
        return board_index, title, star, releasetime, score
    
def save_to_mysql(board_index,title,star, releasetime,score):
    cur = conn.cursor()   #用来获得python执行Mysql命令的方法,操作游标。cur = conn.sursor才是真的执行
    insert_data = "INSERT INTO maoyantop100(board_index,title,star, releasetime,score)" "VALUES(%s,%s,%s,%s,%s)"
    val = (board_index,title,star, releasetime,score)
    cur.execute(insert_data, val)
    conn.commit()

conn=pymysql.connect(host="localhost",user="root", password='180****3940',db='58yuesao',port=3306, charset="utf8")

def main():
    t = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
    urls = ['https://maoyan.com/board/4?offset={}'.format(i) for i in t]
    for url in urls:
        html = get_page_index(url)
        board_index, title, star, releasetime, score = parse_one_page(html)
        save_to_mysql(board_index, title, star, releasetime, score)

if __name__ == "__main__":
    main()
存入数据库

本文存在一个小bug,在存入数据库时很崩溃,只爬取了每页的第一条信息。

我也不知道为什么,没存入数据库时爬取内容是完整的,存入之后就…………解决后会更新到本文。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一些sqlmap的命令,留着备用:https://www.freebuf.com/sectool/164608.h...
    NoelleMu阅读 6,684评论 0 3
  • iPhone和Mac电脑的防丢。 手机实在太重要了,一旦丢失,里面的数据甚至比手机本身还重要。 防丢包括手机里面的...
    飞_168阅读 648评论 0 0
  • 我做好了下一刻遇见真爱的准备,也有勇气面对孑然一生。 过了二十五岁大关,意味着一路奔三,时间真是刹不住脚,悠悠哉哉...
    茶半点阅读 286评论 0 3
  • 三四线城市消费者成为春节旅游生力军。从客源来看,三四线城市经济增速较快,人均收入水平提升使得三四线城市消费者成为今...
    十年一井阅读 104评论 0 0
  • 在结婚的时候,大家的祝福语都是祝你们永结同心。那到底结婚之后有多少对婚姻能永结同心呢?有多少人能够做到家庭工作都协...
    zozo买手日志阅读 275评论 0 0