爬虫练手项目:获取豆瓣评分最高的电影并下载

前期回顾

上篇博文我们学习了Python爬虫的四大库urllibrequestsBeautifulSoup以及selenium
爬虫常用库介绍

  • 学习了urllibrequest的常见用法
  • 学习了使用BeautifulSoup来解析网页以及使用selenium来驱动浏览器

# 我们导入了 web 驱动模块
from selenium import webdriver
# 接着我们创建了一个 Chrome 驱动
driver = webdriver.Chrome()
# 接着使用 get 方法打开百度
driver.get("https://www.baidu.com")
# 获取输入框并且往里面写入我们要搜索的内容
input = driver.find_element_by_css_selector('#kw')
input.send_keys("波多野结衣照片")
# 我们就获取到搜索这个按钮然后点击
button = driver.find_element_by_css_selector('#su')
button.click()

则是上次查看波多老师图片的代码,效果如下

抓取豆瓣电影并保存本地

我们来抓取一下豆瓣上排名前250的电影


import requests
from bs4 import BeautifulSoup
import xlwt

def request_douban(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
    except requests.RequestException:
        return None

book = xlwt.Workbook(encoding='utf-8', style_compression=0)

sheet = book.add_sheet('豆瓣电影Top250', cell_overwrite_ok=True)
sheet.write(0, 0, '名称')
sheet.write(0, 1, '图片')
sheet.write(0, 2, '排名')
sheet.write(0, 3, '评分')
sheet.write(0, 4, '作者')
sheet.write(0, 5, '简介')

n = 1

def save_to_excel(soup):
    list = soup.find(class_='grid_view').find_all('li')

    for item in list:
        item_name = item.find(class_='title').string
        item_img = item.find('a').find('img').get('src')
        item_index = item.find(class_='').string
        item_score = item.find(class_='rating_num').string
        item_author = item.find('p').text
        if (item.find(class_='inq') != None):
            item_intr = item.find(class_='inq').string

        # print('爬取电影:' + item_index + ' | ' + item_name +' | ' + item_img +' | ' + item_score +' | ' + item_author +' | ' + item_intr )
        print('爬取电影:' + item_index + ' | ' + item_name + ' | ' + item_score + ' | ' + item_intr)

        global n

        sheet.write(n, 0, item_name)
        sheet.write(n, 1, item_img)
        sheet.write(n, 2, item_index)
        sheet.write(n, 3, item_score)
        sheet.write(n, 4, item_author)
        sheet.write(n, 5, item_intr)

        n = n + 1

def main(page):
    url = 'https://movie.douban.com/top250?start=' + str(page * 25) + '&filter='
    html = request_douban(url)
    soup = BeautifulSoup(html, 'lxml')
    save_to_excel(soup)

if __name__ == '__main__':

    for i in range(0, 10):
        main(i)

book.save(u'豆瓣最受欢迎的250部电影.csv')

代码分析

首先导入相关库

import requests
# 请求网页库
from bs4 import BeautifulSoup
# 解析网页库
import xlwt
# 与Excel文件交互

定义一个请求网页的函数

def request_douban(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
    except requests.RequestException:
        return None

创建一个存储数据的Excel

book = xlwt.Workbook(encoding='utf-8', style_compression=0)

sheet = book.add_sheet('豆瓣电影Top250', cell_overwrite_ok=True)
sheet.write(0, 0, '名称')
sheet.write(0, 1, '图片')
sheet.write(0, 2, '排名')
sheet.write(0, 3, '评分')
sheet.write(0, 4, '作者')
sheet.write(0, 5, '简介')

n = 1

定义一个将BeautifulSoup到的数据存入Excel的函数

def save_to_excel(soup):
    list = soup.find(class_='grid_view').find_all('li')

    for item in list:
        item_name = item.find(class_='title').string
        item_img = item.find('a').find('img').get('src')
        item_index = item.find(class_='').string
        item_score = item.find(class_='rating_num').string
        item_author = item.find('p').text
        if (item.find(class_='inq') != None):
            item_intr = item.find(class_='inq').string

        # print('爬取电影:' + item_index + ' | ' + item_name +' | ' + item_img +' | ' + item_score +' | ' + item_author +' | ' + item_intr )
        print('爬取电影:' + item_index + ' | ' + item_name + ' | ' + item_score + ' | ' + item_intr)

        global n

        sheet.write(n, 0, item_name)
        sheet.write(n, 1, item_img)
        sheet.write(n, 2, item_index)
        sheet.write(n, 3, item_score)
        sheet.write(n, 4, item_author)
        sheet.write(n, 5, item_intr)

        n = n + 1

定义主函数传入URL并且存储,调用主函数

def main(page):
    url = 'https://movie.douban.com/top250?start=' + str(page * 25) + '&filter='
    html = request_douban(url)
    soup = BeautifulSoup(html, 'lxml')
    save_to_excel(soup)

if __name__ == '__main__':

    for i in range(0, 10):
        main(i)

运行后发现文件夹中多了 “豆瓣最受欢迎的250部电影.csv”这个文件,打开看看

原文地址:https://www.cnblogs.com/BigBears/p/11973496.html

时间: 2024-08-30 06:53:46

爬虫练手项目:获取豆瓣评分最高的电影并下载的相关文章

Python练手项目:20行爬取全王者全英雄皮肤

引言 ? ?王者荣耀大家都玩过吧,没玩过的也应该听说过,作为时下最火的手机MOBA游戏,咳咳,好像跑题了.我们今天的重点是爬取王者荣耀所有英雄的所有皮肤,而且仅仅使用20行Python代码即可完成. ? ?文中源代码在文章末尾,可自行复制粘贴. 准备工作 ? ?爬取皮肤本身并不难,难点在于分析,我们首先得得到皮肤图片的url地址,话不多说,我们马上来到王者荣耀的官网: ? ?我们点击英雄资料,然后随意地选择一位英雄,接着F12打开调试台,找到英雄原皮肤的图片地址: ? ?接着,我们切换一下英雄的

Python之路【第二十四篇】:Python学习路径及练手项目合集

Python学习路径及练手项目合集 Wayne Shi· 2 个月前 参照:https://zhuanlan.zhihu.com/p/23561159 更多文章欢迎关注专栏:学习编程. 本系列Python技术路径中包含入门知识.Python基础.Web框架.基础项目.网络编程.数据与计算.综合项目七个模块.路径中的教程将带你逐步深入,学会如何使用 Python 实现一个博客,桌面词典,微信机器人或网络安全软件等.完成本路径的基础及项目练习,将具备独立的Python开发能力. 完整的Python学

webpack练手项目之easySlide(二):代码分割

Hello,大家好. 在上一篇 webpack练手项目之easySlide(一):初探webpack  中我们一起为大家介绍了webpack的基本用法,使用webpack对前端代码进行模块化打包. 但是乍一看webpack只是将所有资源打包到一个JS文件中而已,并没有做到真正的按需加载,这当然不是我们所想要的. 不急,今天的这一章我们就来一起继续探索webpack的另外一个功能:code split. 1.什么是code split  英文不好,暂且将其翻译为代码分割.也就是我们根据实际业务需求

练手项目之image caption问题记录

小白一个,刚刚费了老大的劲完成一个练手项目--image caption,虽然跑通了,但是评估结果却惨不忍睹.于是贴上大神的作品,留待日后慢慢消化.顺便记录下自己踩坑的一些问题. 先膜拜下大神的作品. 本次项目采用的模型结构如下.一路输入信息是利用VGG16提取的图像特征,另一路输入信息是利用LSTM提取的单词串特征,输出是预测的下一个单词.即模型的功能是,在给定图像特征和caption前面若干个单词的情况下,能预测出caption的下一个单词:所以循环若干次后即可得到一句完整的caption.

前端练手项目

前端学习还是很有趣的,可以较快的上手然后自己开发一些好玩的项目来练手,网上也可以一抓一大把关于前端开发的小项目,可是还是有新手在学习的时候不知道可以做什么,以及怎么做,因此,就整理了一些前端项目教程,希望可以帮助正在学习前端的小伙伴.为了方便阅读,大概把前端可以做的项目分为三类: 游戏类 实用类 好玩类 然后依次推荐一些项目教程,想要学习的小伙伴可以看看~ 游戏类 其实很多常见的小游戏都是纯前端开发出来的,比如曾经风靡的2048.别踩白块啊等等,简单有趣,对于初学者来说,这些小游戏是非常不错的练

推荐的阅读及练手项目

关于作者: He is an expert in numerous languages including .NET, PHP, C/C++, Java and more 推荐阅读(Software development, Desktop, Web) http://www.coderslexicon.com/recommended-reading/ 推荐练手项目: http://blog.jobbole.com/49762/, 对应的电子书名叫The Programmers Idea Book

20个Java练手项目,献给嗜学如狂的人

给大家推荐一条由浅入深的JAVA学习路径,首先完成 Java基础.JDK.JDBC.正则表达式等基础实验,然后进阶到 J2SE 和 SSH 框架学习.最后再通过有趣的练手项目进行巩固. JAVA基础 Java编程语言(新版 2. Java进阶之设计模式 3. JDK 核心 API 4. MySQL 基础课程 5. 正则表达式基础 6. JDBC 入门教程 J2SE & SSH框架 7. Java 函数式编程 8. J2SE网络通信实践 9. Struts框架教程 10. Hibernate框架教

爬虫练手,爬取新浪双色彩,信息并进行分析

爬虫练手,爬取新浪双色彩,信息并进行分析 import requests from lxml.html import etree url = 'http://zst.aicai.com/ssq/betOrder/' response = requests.get(url) response_html = etree.HTML(response.text) text_path = '/html/body/div[7]/form/div[2]/table/tbody/tr/td/text()' da

70个Python练手项目

前言: 不管学习那门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说大家都知道学编程语言一定要做项目才行. 这里整理了70个Python实战项目列表,都有完整且详细的教程,你可以从中选择自己想做的项目进行参考学习练手,你也可以从中寻找灵感去做自己的项目. 70个Python项目列表: 1.[Python 图片转字符画]2.[200行Python代码实现2048]3.[Python3 实现火车票查询工具]4.[高德API+Python解决租房问题 ]5.[Python3 色情图