Python爬虫入门之 如何在豆瓣中获取自己喜欢的TOP N电影信息

什么是爬虫

按照一定规则自动的获取互联网上的信息(如何快速有效的利用互联网上的大量信息)

爬虫的应用

  • 搜索引擎(Google、百度、Bing等搜索引擎,辅助人们检索信息)
  • 股票软件(爬取股票数据,帮助人们分析决策,进行金融交易)
  • Web扫描(需要对网站所有的网页进行漏洞扫描)
  • 获取某网站最新文章收藏
  • 爬取天气预报
  • 爬取漂亮mm照片

基础知识

1.HTTP 协议
客户端发起请求,服务器接收到请求后返回格式化的数据,客户端接收数据,并进行解析和处理

2.HTML(超文本标记语言)

html.png

3.Python

  • 基础语法&常用系统模块
  • 第三方模块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.程序启动

start.png

2.运行结果

result.png

手动搜索TOP N电影信息

1.获取电影列表

list.png

2.获取电影详情超链接

link.png

3.获取电影详情

info.png

代码走读

1.程序启动

main.png

2.查找电影列表

list.png

3.查找电影详情

info.png

4.写入csv文件

csv.png

源码


#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')

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 爬虫文章 in 简书程序员专题: like:128-Python 爬取落网音乐 like:127-【图文详解】py...
    喜欢吃栗子阅读 22,054评论 4 411
  • 1 前言 作为一名合格的数据分析师,其完整的技术知识体系必须贯穿数据获取、数据存储、数据提取、数据分析、数据挖掘、...
    whenif阅读 18,107评论 45 523
  • 声明:本文讲解的实战内容,均仅用于学习交流,请勿用于任何商业用途! 一、前言 强烈建议:请在电脑的陪同下,阅读本文...
    Bruce_Szh阅读 12,788评论 6 28
  • 昨晚十一点多,我在房间准备睡觉,阿肥在客厅吹头发,突然有人敲门,阿肥想去开门,龟仔说这么晚不能随便开门,先问问是谁...
    吃货霞阅读 639评论 0 0
  • 迎着北方来风的方向 我决心要种一株白色的桔梗 勤于施肥,勤于浇水 在星夜和夕阳里陪它眺望 我这般殷勤 不为了开花的...
    小小小洛阅读 507评论 0 0