使用Beautiful Soup和requests爬取豆瓣电影top250

您所在的位置:网站首页 豆瓣250爬虫 使用Beautiful Soup和requests爬取豆瓣电影top250

使用Beautiful Soup和requests爬取豆瓣电影top250

2024-07-06 00:16:07| 来源: 网络整理| 查看: 265

1. Beautiful Soup

前面介绍了Beautiful Soup的基本使用方法,请参考文章 爬虫利器BeautifulSoup之CSS选择器的基本使用和Beautiful Soup之find()和find_all()的基本使用,现在搞个实例来验证一下。就以爬取豆瓣电影信息为例吧。

2. 分析请求链接

打开豆瓣电影top250首页信息如下: 在这里插入图片描述 下滑到最下方,可以看到一共有10页,F12或右键检查每页的具体链接,可以发现如下规律:

#第1页: https://movie.douban.com/top250?start=0&filter= #第2页: https://movie.douban.com/top250?start=25&filter= #第3页: https://movie.douban.com/top250?start=50&filter= #第4页: https://movie.douban.com/top250?start=75&filter= ...... #第10页: https://movie.douban.com/top250?start=225&filter=

不难发现每一页的链接不同之处在于start参数值不一样。随着页数的增加,start每次增加25,一直到最后一页为225,故可以轻松的构造前十页的链接。

base_url = 'https://movie.douban.com/top250?start={}&filter=' url_list = [base_url.format(i) for i in range(0, 250, 25)] 3.使用requests获取每个链接的网页源码

只需伪装一下请求头即可,代码如下:

def get_html(url): header = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36' } return requests.get(url=url, headers=header).text 4. 使用Beautiful Soup解析网页

使用Beautiful Soup提取电影信息,提取的信息包括:电影图片链接src、电影详情页链接href、电影标题title、导演和主演信息以及上映年份等direct、电影评分score、电影评论人数comment_num、电影概述quote等。

def parser_html(response): soup = BeautifulSoup(response, 'html.parser') # 获取包含25个电影信息的li标签 li_list = soup.select('ol.grid_view li') info = [] # 解析每一个li标签中的电影信息 for li in li_list: src = li.select('img')[0].get('src') href = li.select('a[href]')[0].get('href') title = li.select('span')[0].string direct = li.select('div.bd p')[0].get_text().replace('\n', '').replace(' ', '') score = li.select('span.rating_num')[0].string comment_num = li.select('div.star span')[3].string # 因为后面几部电影没有概述quote,会发生数组越界,这里捕获一下。 try: quote = li.select('span.inq')[0].string except IndexError: quote = None print(src, href, title, direct, score, comment_num, quote) info.append({'src': src, 'href': href, 'title': title, 'direct': direct, 'score': score, 'comment_num': comment_num, 'quote': quote}) # 每一页的25个电影信息都在info变量中了。 return info 5.把信息保存至mongodb数据库

使用mongo数据库保存电影信息,这里把信息保存到Douban数据库,集合名称为douban.

def save_info_to_mongo(info): client = pymongo.MongoClient('localhost', 27017) collection = Collection(Database(client, 'Douban'), 'douban') for info in info: collection.insert_one(info) client.close() 6.全部代码如下

没有安装mongo数据库的可以不使用save_info_to_mongo函数,或者自己使用其他数据库保存一下也可以!

import pymongo import requests from bs4 import BeautifulSoup from pymongo.database import Database from pymongo.collection import Collection def get_html(url): header = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36' } return requests.get(url=url, headers=header).text def parser_html(response): soup = BeautifulSoup(response, 'html.parser') li_list = soup.select('ol.grid_view li') info = [] for li in li_list: src = li.select('img')[0].get('src') href = li.select('a[href]')[0].get('href') title = li.select('span')[0].string direct = li.select('div.bd p')[0].get_text().replace('\n', '').replace(' ', '') score = li.select('span.rating_num')[0].string comment_num = li.select('div.star span')[3].string try: quote = li.select('span.inq')[0].string except IndexError: quote = None print(src, href, title, direct, score, comment_num, quote) info.append({'src': src, 'href': href, 'title': title, 'direct': direct, 'score': score, 'comment_num': comment_num, 'quote': quote}) return info def save_info_to_mongo(info): client = pymongo.MongoClient('localhost', 27017) collection = Collection(Database(client, 'Douban'), 'douban') for info in info: collection.insert_one(info) client.close() if __name__ == '__main__': base_url = 'https://movie.douban.com/top250?start={}&filter=' url_list = [base_url.format(i) for i in range(0, 250, 25)] for page_url in url_list: info = parser_html(get_html(page_url)) # 没有mongo数据库就把这一行注释一下 save_info_to_mongo(info) 7.成果展示

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭