豆瓣电影TOP250和书籍TOP250爬虫
豆瓣电影 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()
总结
- 点击查看我的Github
- 点击查看我的个人Blog
- 日拱一卒,不期速成
以上直接贴出了代码,这是很简单的两段代码,主要用到了 requests 库和 beautifulsoup 库,需要的可以直接拿去,或者直接去我的 GIthub上拿 movies.txt 和 good_books.txt
豆瓣电影TOP250和书籍TOP250爬虫的更多相关文章
- 练习:一只豆瓣电影TOP250的爬虫
练习:一只豆瓣电影TOP250爬虫 练习:一只豆瓣电影TOP250爬虫 ①创建project ②编辑items.py import scrapyclass DoubanmovieItem(scrapy ...
- python3 爬虫---爬取豆瓣电影TOP250
第一次爬取的网站就是豆瓣电影 Top 250,网址是:https://movie.douban.com/top250?start=0&filter= 分析网址'?'符号后的参数,第一个参数's ...
- scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250
scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250 前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大 ...
- 一起学爬虫——通过爬取豆瓣电影top250学习requests库的使用
学习一门技术最快的方式是做项目,在做项目的过程中对相关的技术查漏补缺. 本文通过爬取豆瓣top250电影学习python requests的使用. 1.准备工作 在pycharm中安装request库 ...
- Scrapy爬虫(4)爬取豆瓣电影Top250图片
在用Python的urllib和BeautifulSoup写过了很多爬虫之后,本人决定尝试著名的Python爬虫框架--Scrapy. 本次分享将详细讲述如何利用Scrapy来下载豆瓣电影To ...
- 零基础爬虫----python爬取豆瓣电影top250的信息(转)
今天利用xpath写了一个小爬虫,比较适合一些爬虫新手来学习.话不多说,开始今天的正题,我会利用一个案例来介绍下xpath如何对网页进行解析的,以及如何对信息进行提取的. python环境:pytho ...
- python爬虫 Scrapy2-- 爬取豆瓣电影TOP250
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- Python爬虫----抓取豆瓣电影Top250
有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...
- Python爬虫入门:爬取豆瓣电影TOP250
一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...
随机推荐
- Python模块之snmp-cmds,easysnmp
一.简介 snmp-cmds模块通过SNMP与目标设备进行通信,此模块适用于windows,此模块是基于系统已安装了net-snmp环境easysnmp模块通过SNMP与谬表设备进行通信,此模块用于l ...
- Springboot源码分析之EnableAspectJAutoProxy
摘要: Spring Framwork的两大核心技术就是IOC和AOP,AOP在Spring的产品线中有着大量的应用.如果说反射是你通向高级的基础,那么代理就是你站稳高级的底气.AOP的本质也就是大家 ...
- SpringBoot进阶教程(六十一)intellij idea project下建多个module搭建架构(下)
在上一篇文章<SpringBoot进阶教程(六十)intellij idea project下建多个module(上)>中,我们已经介绍了在intellij idea中创建project之 ...
- N*N矩阵的旋转 不开辟新空间
/* N*N矩阵的旋转 不开辟新空间 programmer:qpz time:2014-11-09 */ #include <iostream> using namespace std; ...
- odoo通过actions.client进行自定义页面
一.使用原因 由于odoo自带页面在项目开发过程中无法满足使用,需要使用到动作ir.actions.client进行自定义视图的开发,实现自定义的xml视图开发. 二.实现目标 三.开发过程 1.项目 ...
- jquery实现表格导入到Excel(加图片)
话不多说直接上代码 第一步:导入jquery的插件https://github.com/rainabba/jquery-table2excel HTML部分: 第二步:添加一个按钮 <but ...
- Hugo
快速开始 安装Hugo 1.二进制安装(推荐:简单.快速) 到 Hugo Releases 下载对应的操作系统版本的Hugo二进制文件(hugo或者hugo.exe) Mac下直接使用 ==Homeb ...
- Badboy - 从excel中读取数据
参考: http://leafwf.blog.51cto.com/872759/1119161 http://www.51testing.com/html/00/130600-1367743.html ...
- SDU暑期集训排位(5)
SDU暑期集训排位(5) A. You're in the Army Now 题意 类似选志愿.每个人有 mark,有优先级从高到低的志愿. 做法 定睛一看,鲨鼻题.然后 WA. 为什么会 WA 呢? ...
- POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...