豆瓣电影 TOP250 和书籍 TOP250 爬虫

最近开始玩 Python , 学习爬虫相关知识的时候,心血来潮,爬取了豆瓣电影TOP250 和书籍TOP250, 这里记录一下自己玩的过程。

电影 TOP250 爬虫
import requests
from bs4 import BeautifulSoup
import time def getlist(list_url):
time.sleep(2)
res = requests.get(list_url)
soup = BeautifulSoup(res.text, 'html.parser')
movie_list = soup.select('.grid_view li')
for m in movie_list:
rank = m.select('em')[0].text
score = m.select('.rating_num')[0].text
title = m.select('.title')[0].text
direct = m.select('.info .bd p')[0].text.strip()
actor = '\n主演:'.join(direct.split(' 主演:'))
director = '年代:'.join(actor.split(' '))
if m.select('.inq'):
comments = m.select('.inq')[0].text.strip()
else:
comments = 'None'
movie.append(
'排名: ' + rank + '\n'
+ '评分: ' + score + '\n'
+ '片名: ' + title + '\n'
+ director + '\n'
+ '评论: ' + comments + '\n'
+ '\n')
if soup.select('.next a'):
asoup = soup.select('.next a')[0]['href']
next_page = seed_url + asoup
getlist(next_page)
else:
print('结束')
return movie def write(movies):
with open('movie.txt', 'w', encoding='utf8') as m:
for a in movies:
m.write(a) def main():
write(getlist(seed_url))
pass if __name__ == '__main__':
seed_url = 'https://movie.douban.com/top250'
movie = []
main()
书籍 TOP250 爬虫
import bs4
import requests
import re
from bs4 import BeautifulSoup
from operator import itemgetter def getHtmlText(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "" def parserText(text, book_list):
soup = BeautifulSoup(text, 'html.parser')
for table in soup('table', {'width': '100%'}):
if isinstance(table, bs4.element.Tag):
tds = table.find('tr')('td')
divs = tds[1]('div')
content = {}
for div in divs:
if isinstance(div, bs4.element.Tag):
if div.find('a'):
name = div.find('a').attrs['title']
content.update({"书名": name})
if div.select('.rating_nums'):
score = div.select('.rating_nums')[0].text
content.update({"评分": score})
if div.select('.pl'):
people_num = div.select('.pl')[0].text
regex = re.compile(r'[\d]{1,10}')
content.update({"评价人数": regex.findall(people_num)[0]}) ps = tds[1]('p')
for p in ps:
if isinstance(p, bs4.element.Tag):
if p.attrs['class'][0] == 'quote':
description = p.find('span').string
content.update({"介绍": description})
if p.attrs['class'][0] == 'pl':
author = p.string
content.update({"作者信息": author}) book_list.append(content) next_books = soup.find('span', {'class': 'next'})
if next_books.find('a'):
a = next_books.find('a').attrs['href']
text = getHtmlText(a)
parserText(text, books) return book_list def sortedBookTop250(book_list):
tmp = sorted(book_list, key=itemgetter('评分'), reverse=True)
for i in range(len(tmp)):
tmp[i].update({"排名": i + 1})
return tmp def writeToFile(book_list):
with open('good_books.txt', 'w', encoding='utf8') as book_file:
for book in book_list:
for key, value in book.items():
book_file.write(f'{key}:{value}\n')
book_file.write('\n')
pass def main():
text = getHtmlText(seed_url)
book_list = parserText(text, books)
writeToFile(sortedBookTop250(book_list))
pass if __name__ == '__main__':
seed_url = "https://book.douban.com/top250"
books = []
main()

总结

以上直接贴出了代码,这是很简单的两段代码,主要用到了 requests 库和 beautifulsoup 库,需要的可以直接拿去,或者直接去我的 GIthub上拿 movies.txtgood_books.txt

豆瓣电影TOP250和书籍TOP250爬虫的更多相关文章

  1. 练习:一只豆瓣电影TOP250的爬虫

    练习:一只豆瓣电影TOP250爬虫 练习:一只豆瓣电影TOP250爬虫 ①创建project ②编辑items.py import scrapyclass DoubanmovieItem(scrapy ...

  2. python3 爬虫---爬取豆瓣电影TOP250

    第一次爬取的网站就是豆瓣电影 Top 250,网址是:https://movie.douban.com/top250?start=0&filter= 分析网址'?'符号后的参数,第一个参数's ...

  3. scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250

    scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250 前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大 ...

  4. 一起学爬虫——通过爬取豆瓣电影top250学习requests库的使用

    学习一门技术最快的方式是做项目,在做项目的过程中对相关的技术查漏补缺. 本文通过爬取豆瓣top250电影学习python requests的使用. 1.准备工作 在pycharm中安装request库 ...

  5. Scrapy爬虫(4)爬取豆瓣电影Top250图片

      在用Python的urllib和BeautifulSoup写过了很多爬虫之后,本人决定尝试著名的Python爬虫框架--Scrapy.   本次分享将详细讲述如何利用Scrapy来下载豆瓣电影To ...

  6. 零基础爬虫----python爬取豆瓣电影top250的信息(转)

    今天利用xpath写了一个小爬虫,比较适合一些爬虫新手来学习.话不多说,开始今天的正题,我会利用一个案例来介绍下xpath如何对网页进行解析的,以及如何对信息进行提取的. python环境:pytho ...

  7. python爬虫 Scrapy2-- 爬取豆瓣电影TOP250

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  8. Python爬虫----抓取豆瓣电影Top250

    有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...

  9. Python爬虫入门:爬取豆瓣电影TOP250

    一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...

随机推荐

  1. poium测试库之JavaScript API封装原理

    poium一直我在维护的一个开源项目,它的定位是以极简的方式在自动化项目中Page Objects设计模式.我在之前的文章中也有介绍. 本篇文章主要介绍一个JavaScript元素操作的封装原理. 为 ...

  2. ​综述 | SLAM回环检测方法

    本文作者任旭倩,公众号:计算机视觉life成员,由于格式原因,公式显示可能出问题,建议阅读原文链接:综述 | SLAM回环检测方法 在视觉SLAM问题中,位姿的估计往往是一个递推的过程,即由上一帧位姿 ...

  3. Spring MVC内容协商实现原理及自定义配置【享学Spring MVC】

    每篇一句 在绝对力量面前,一切技巧都是浮云 前言 上文 介绍了Http内容协商的一些概念,以及Spring MVC内置的4种协商方式使用介绍.本文主要针对Spring MVC内容协商方式:从步骤.原理 ...

  4. Java并发编程知识点总结Volatile、Synchronized、Lock实现原理

    Volatile关键字及其实现原理 在多线程并发编程中,Volatile可以理解为轻量级的Synchronized,用volatile关键字声明的变量,叫做共享变量,其保证了变量的“可见性”以及“有序 ...

  5. Unity/C#基础复习(5) 之 浅析观察者、中介者模式在游戏中的应用与delegate原理

    参考资料 [1] <Unity 3D脚本编程 使用C#语言开发跨平台游戏>陈嘉栋著 [2] @张子阳[C#中的委托和事件 - Part.1] http://www.tracefact.ne ...

  6. python学习——面向对象编程

    关于python面向对象编程,请参考: https://blog.csdn.net/zhoudaxia/article/details/23341261

  7. 非域环境下SQL Server搭建Mirror(镜像)的详细步骤

    1.测试验证环境 服务器角色 机器名 IP SQL Server Ver 主体服务器 WIN-TestDB4O 172.83.XXX.XXX SQL Server 2012 - 11.0.5058.0 ...

  8. 礼盒抖动动画(CocosCreator)

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       这个月还有一天了,别问我为什么是一天,996,懂吗?项目是做不完了,策划又加新功能,又不能安静的改bug了.又是动画 ...

  9. Git 上传本地项目到Github

    前提: 安装Git 注册并拥有Github账号 目录: 初始化本地目录位Git仓库 Github上创建仓库 本地生成SSH key,并添加到Github上 本地项目管理Github上远程项目 详细步骤 ...

  10. 微擎 人人商城 导出excel表分析

    在 数据处理上 ,有很多时候需要导出excel表  来当报表, 等 ,  php  人人商城导出报表过程简单分析 在导出时候发现 ca('statistics.order.export'); 出于好奇 ...