Python爬虫入门 之 如何在豆瓣中获取自己喜欢的TOP N电影信息
按照一定规则自动的获取互联网上的信息(如何快速有效的利用互联网上的大量信息)
爬虫的应用
- 搜索引擎(Google、百度、Bing等搜索引擎,辅助人们检索信息)
- 股票软件(爬取股票数据,帮助人们分析决策,进行金融交易)
- Web扫描(需要对网站所有的网页进行漏洞扫描)
- 获取某网站最新文章收藏
- 爬取天气预报
- 爬取漂亮mm照片
基础知识
1.HTTP 协议
客户端发起请求,服务器接收到请求后返回格式化的数据,客户端接收数据,并进行解析和处理
2.HTML(超文本标记语言)

- 基础语法&常用系统模块
- 第三方模块requests,pyquery使用
安装:
pip install requests pip install pyquery
requests模块使用:
#requests(发起HTTP请求,并获取结果)
response = requests.get('http://localhost:9999/index.html')
response = requests.post()
print response.content
pyquery模块使用:
page = PyQuery(html)
选择器
tag: page('title')
id: page('#job_1')
class: page('.job')
复合选择器
page('div#job_1')
page('div.job')
子选择器
page('div#job_1 li')
page('div#job_1 > li')
page('div#job_1').find('li')
page('div#job_1').children('li')
获取标签内的html page('div#job_1').html()
获取标签内的文本 page('div#job_1').text()
获取标签属性 page('div#job_1').attr['id']
csv模块使用:
writer = csv.writer() writer.writerow() writer.writerows()
程序运行
1.程序启动

2.运行结果

手动搜索TOP N电影信息
1.获取电影列表

2.获取电影详情超链接

3.获取电影详情

代码走读
1.程序启动

2.查找电影列表

3.查找电影详情

4.写入csv文件

源码
#encoding: utf-8
import requests
from pyquery import PyQuery as pq
import csv
attrs = [u'超链接', u'名称', u'评分', u'导演', u'编剧', u'主演', u'类型', u'制片国家/地区', u'语言', u'上映日期', u'片长', u'又名', u'IMDb链接']
'''
获取电影详情
'''
def attch_info(info, text, key, value):
text = text.strip(' ')
if text:
if text in attrs:
if key and value:
info[key] = ' '.join(value)
key = text
value = []
else:
value.append(text)
return info, key, value
'''
解析电影信息
'''
def parse_movie_info(text, info):
key = None
value = []
for e in text.split(':'):
e = e.strip()
pos = e.rfind(' ')
if -1 == pos:
info, key, value = attch_info(info, e, key, value)
else:
info, key, value = attch_info(info, e[:pos], key, value)
info, key, value = attch_info(info, e[pos:], key, value)
if key not in info:
info[key] = ' '.join(value)
'''
解析电影页面
'''
def crawl_info(url):
info = {}
print url
response = requests.get(url)
page = pq(response.content)
content = page('div#content').eq(0)
info[u'超链接'] = url
info[u'名称'] = content('h1 span').eq(0).text()
info[u'评分'] = content('div.rating_wrap strong.rating_num').text()
info_text = content('div#info').text()
parse_movie_info(info_text, info)
return info
'''
获取电影列表
'''
def crawl(query_text, count):
start = 0
rt_list = []
isStop = False
url = 'https://movie.douban.com/subject_search?start={start}&search_text={query_text}&cat=1002'
while True:
response = requests.get(url.format(query_text=query_text.encode('utf-8', 'ignore'), start=start))
page = pq(response.content)
links = page('div#content table a').not_('.nbg')
if len(links) == 0:
isStop = True
for link in links:
href = pq(link).attr['href']
rt_list.append(crawl_info(href))
start += 1
if len(rt_list) >= count:
isStop = True
break
if isStop:
break
return rt_list
'''
写入文件
'''
def write_to_file(lines, path):
with open(path, 'wb') as fhandler:
writer = csv.writer(fhandler)
writer.writerow(map(lambda x: x.encode('gbk', 'ignore'), attrs))
for line in lines:
row = []
for key in attrs:
row.append(line.get(key, '').encode('gbk', 'ignore'))
writer.writerow(row)
if __name__ == '__main__':
query_text = raw_input(u"请输入关键字:".encode('utf-8', 'ignore'))
count = raw_input(u"请输入爬取得数据量:".encode('utf-8', 'ignore'))
query_text = query_text.strip().decode('utf-8') if query_text.strip() else u'长城'
count = int(count) if count.isdigit() else 10
print u'关键字:{query_text}, 数量:{count}'.format(query_text=query_text, count=count)
rt_list = crawl(query_text, count)
write_to_file(rt_list, 'result.csv')
作者:imsilence
链接:https://www.jianshu.com/p/7eceedb39f3b
Python爬虫入门 之 如何在豆瓣中获取自己喜欢的TOP N电影信息的更多相关文章
- 如何用Python在豆瓣中获取自己喜欢的TOP N电影信息
一.什么是 Python Python (蟒蛇)是一门简单易学. 优雅健壮. 功能强大. 面向对象的解释型脚本语言.具有 20+ 年发展历史, 成熟稳定. 具有丰富和强大的类库支持日常应用. 1989 ...
- Python爬虫入门:爬取豆瓣电影TOP250
一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...
- Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- python爬虫入门-开发环境与小例子
python爬虫入门 开发环境 ubuntu 16.04 sublime pycharm requests库 requests库安装: sudo pip install requests 第一个例子 ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分
1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- Python爬虫入门之正则表达式
在前面我们已经搞定了怎样获取页面的内容,不过还差一步,这么多杂乱的代码夹杂文字我们怎样把它提取出来整理呢?下面就开始介绍一个十分强大的工具,正则表达式! 1.了解正则表达式 正则表达式是对字符串操作的 ...
随机推荐
- 工具-github在linux下面没有git push报错
time: 2015/12/25 1. 描述: error: The requested URL returned error: 403 Forbidden while accessing https ...
- 铁乐学python_day03-作业
1.有变量name = "aleX leNb" 完成如下操作: 移除name变量对应的值两边的空格,并输出处理结果 n1 = name.strip() print(n1) 结果:a ...
- Ogre学习教程:Ogre1.8.1+VS2010环境配置2(转)
之前按照前面一篇文章提到的部署了ogre1.9,后来查询资料,有的提到关于vs2010还是安装ogre1.8比较稳定,由于是小白,又比对着几篇文章重新配置了一遍. 从一开始的什么都不会,到现在能知道每 ...
- eclipse缓慢了么?
我的eclipse突然变得无比缓慢,javaw.exe的cpu使用率高达85%! 可是我什么也没做啊.项目组的其他同事询问过后,也没有谁修改了eclipse的配置文件(.setting文件夹 .cl ...
- 解决Failed to load the JNI shared library xxx/xxx/jvm.dll 错误
原因:jdk发生变化(新装了32位jdk),eclipse在启动时使用了 系统环境变量中的jdk路径(32位). 解决:只要把旧的64位的jre路径指定给eclipse启动文件即可. 在eclipse ...
- 026.2 网络编程 UDP聊天
实现,通过socket对象 ##############################################################需求建立UDP发送端:###思路:1.建立可以实 ...
- socket.io+angular.js+express.js做个聊天应用(二)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/www19940501a/article/details/27585321 接着上一篇 我用的开发工具 ...
- 【转】 java中Class对象详解和类名.class, class.forName(), getClass()区别
Class对象的生成方式如下: 1.类名.class 说明: JVM将使用类装载器, 将类装入内存(前提是:类还没有装入内存),不做类的初始化工作.返回Class的对象 2.Cla ...
- 【转】matplotlib制图——图例legend
转自:https://www.cnblogs.com/alimin1987/p/8047833.html import matplotlib.pyplot as pltimport numpy as ...
- block本质探寻七之内存管理
说明: <1>阅读本问,请参照block前述文章加以理解: <2>环境:ARC: <3>变量类型:基本数据类型或者对象类型的auto局部变量: 一.三种情形 //代 ...