爬虫项目爬取猫眼电影TOP100电影信息

项目内容来自:https://github.com/Germey/MaoYan/blob/master/spider.py

由于其中需要爬取的包含电影名字、电影海报图片、演员、上映时间等众多信息,正则表达式代码较为复杂

在parse_one_page(html)获取HTML文本print(html)后得到以下信息:

#划线为匹配内容
<dd>
<i class="board-index board-index-1">1</i> #电影排名
<a href="fim/1203"title="霸王别姬" class="image-link"
data-act"boarditem-click" data-val="{movieId:1203}">
<img src="//ms0.meituan.net/mywww/image/Loading_2.e3d934bf.png" alt="" class="poster-default"/>
<img data-src="http://p1.meeituan.net/movie/20803f59291c47e1e116c11963cee19e68711.ing160w_22h_1e_1c" alt="霸王别姬” class="board-img" /> #image
</a>
<div class="board-item-main">
<div class="board-item-content">
<diy classamovie-item-info>
<p Class="name"><a href"/ films/1293 title-"露王別姬”data-act=" boorditem-cltck"data-val="{ moved:1283]">霸王别姬</a></p> #title、actor和name
<p class-star>
主演:张国荣,张丰毅,巩俐
</p>
<p class"releasetime">上映时间:1993-01-01〔中国香港)</p> </div> #time
<div class="movie-item-numher score-num">
<p class=score><i class="integer">9.</i><i class="fraction">6</i></p></div> #integer和fraction分数

详解正则表达式

pattern = re.compile(
'<dd>.*?board-index.*?>(\d+)</i> .*?data-src="(.*?)".*?name"><a' #匹配电影排名index和电影海报image
+
'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>' #匹配电影名name、明星演员actor和上映时间time
+
'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>‘ #匹配integer和电影评分fraction
, re.S)

正则表达式为:

def parse_one_page(html):
pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a'
+'.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>'
+'.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)
items = re.findall(pattern, html)
for item in items:
yield {
'index': item[0],
'image': item[1],
'title': item[2],
'actor': item[3].strip()[3:],
'time': item[4].strip()[5:],
'score': item[5]+item[6]
}

匹配成功之后输出的result.txt结果:

{"title": "霸王别姬", "image": "http://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c", "actor": "张国荣,张丰毅,巩俐", "time": "1993-01-01(中国香港)", "score": "9.6", "index": "1"}
{"title": "肖申克的救赎", "image": "http://p0.meituan.net/movie/__40191813__4767047.jpg@160w_220h_1e_1c", "actor": "蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿", "time": "1994-10-14(美国)", "score": "9.5", "index": "2"}
{"title": "本杰明·巴顿奇事", "image": "http://p0.meituan.net/movie/48/2207789.jpg@160w_220h_1e_1c", "actor": "布拉德·皮特,凯特·布兰切特,塔拉吉·P·汉森", "time": "2008-12-25(美国)", "score": "8.8", "index": "71"}
{"title": "哈利·波特与死亡圣器(下)", "image": "http://p0.meituan.net/movie/76/612928.jpg@160w_220h_1e_1c", "actor": "丹尼尔·雷德克里夫,鲁伯特·格林特,艾玛·沃森", "time": "2011-08-04", "score": "9.0", "index": "72"}
{"title": "这个杀手不太冷", "image": "http://p0.meituan.net/movie/fc9d78dd2ce84d20e53b6d1ae2eea4fb1515304.jpg@160w_220h_1e_1c", "actor": "让·雷诺,加里·奥德曼,娜塔莉·波特曼", "time": "1994-09-14(法国)", "score": "9.5", "index": "3"}
{"title": "大话西游之大圣娶亲", "image": "http://p0.meituan.net/movie/b429501a792ae227deaa16bc25c2e07a122042.jpg@160w_220h_1e_1c", "actor": "周星驰,朱茵,罗家英", "time": "2014-10-24", "score": "9.4", "index": "73"}
{"title": "致命魔术", "image": "http://p0.meituan.net/movie/12/2130469.jpg@160w_220h_1e_1c", "actor": "休·杰克曼,克里斯蒂安·贝尔,迈克尔·凯恩", "time": "2006-10-20(美国)", "score": "8.8", "index": "61"}
{"title": "罗马假日", "image": "http://p0.meituan.net/movie/23/6009725.jpg@160w_220h_1e_1c", "actor": "格利高利·派克,奥黛丽·赫本,埃迪·艾伯特", "time": "1953-09-02(美国)", "score": "9.1", "index": "4"}
{"title": "阿甘正传", "image": "http://p0.meituan.net/movie/53/1541925.jpg@160w_220h_1e_1c", "actor": "汤姆·汉克斯,罗宾·怀特,加里·西尼斯", "time": "1994-07-06(美国)", "score": "9.4", "index": "5"}
{"title": "十二怒汉", "image": "http://p0.meituan.net/movie/86/2992612.jpg@160w_220h_1e_1c", "actor": "亨利·方达,李·科布,马丁·鲍尔萨姆", "time": "1957-04-13(美国)", "score": "9.1", "index": "62"}
{"title": "倩女幽魂", "image": "http://p0.meituan.net/movie/85/3966083.jpg@160w_220h_1e_1c", "actor": "张国荣,王祖贤,午马", "time": "2011-04-30", "score": "9.1", "index": "74"}
#省略

Python正则表达式匹配猫眼电影HTML信息的更多相关文章

  1. python学习(23)requests库爬取猫眼电影排行信息

    本文介绍如何结合前面讲解的基本知识,采用requests,正则表达式,cookies结合起来,做一次实战,抓取猫眼电影排名信息. 用requests写一个基本的爬虫 排行信息大致如下图 网址链接为ht ...

  2. requests+正则表达式提取猫眼电影top100

    #requests+正则表达式提取猫眼电影top100 import requests import re import json from requests.exceptions import Re ...

  3. Python之爬虫-猫眼电影

    Python之爬虫-猫眼电影 #!/usr/bin/env python # coding: utf-8 import json import requests import re import ti ...

  4. [转载]Python正则表达式匹配反斜杠'\'问题

    转载自csdnblog:Python正则表达式匹配反斜杠'\'问题 在学习Python正则式的过程中,有一个问题一直困扰我,如何去匹配一个反斜杠(即“\”)? 一.引入 在学习了Python特殊字符和 ...

  5. Python: 正则表达式匹配反斜杠 "\"

    Python正则表达式匹配反斜杠 "\" eg: >>>a='w\w\w' 'w\\w\\w' #  打印出来的 "\\" 被转义成 一个反斜 ...

  6. Python反爬:利用js逆向和woff文件爬取猫眼电影评分信息

    首先:看看运行结果效果如何! 1. 实现思路 小编基本实现思路如下: 利用js逆向模拟请求得到电影评分的页面(就是猫眼电影的评分信息并不是我们上述看到的那个页面上,应该它的实现是在一个页面上插入另外一 ...

  7. 爬虫系列(1)-----python爬取猫眼电影top100榜

    对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...

  8. 1.requests+正则表达式爬猫眼电影TOP100

    import requests from requests.exceptions import RequestException def get_one_page(url):try: response ...

  9. python爬取猫眼电影top100

    最近想研究下python爬虫,于是就找了些练习项目试试手,熟悉一下,猫眼电影可能就是那种最简单的了. 1 看下猫眼电影的top100页面 分了10页,url为:https://maoyan.com/b ...

随机推荐

  1. How to solve the problem : &quot;You have been logged on with a temporary profile&quot;

    /*By Jiangong SUN*/ I've encountered a problem in one server, which is : Every time I login into the ...

  2. 【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)

    一.为什么要使程序在后台执行 我们计算的程序都是周期很长的,通常要几个小时甚至一个星期.我们用的环境是用putty远程连接到日本Linux服务器.所以使程序在后台跑有以下三个好处: 1:我们这边是否关 ...

  3. 由于OBJ模型的读取引起的Release无问题Debug卡死问题

    有些时候会遇到Release版本正常运行,但是Debug无法运行甚至崩溃,原因有很多种,这里记录一下由于模型文件读取引起的Debug问题. 项目中需要读取一个obj模型文件,30M左右,Debug模式 ...

  4. g++参数介绍

    转自http://www.cnblogs.com/lidan/archive/2011/05/25/2239517.html [介绍] gcc and g++分别是gnu的c & c++编译器 ...

  5. 《转》windows下通过cmd切换python2和python3版本

    当电脑中同时安装了python2和python3时,往往会由切换版本的需求.那么如何通过cmd命令行做到呢? 方法:修改python.exe的文件名 举个栗子: 我的电脑中同时安装了py2.7.10和 ...

  6. oracle闪回数据

    方法一 数据删除了: select * from  t_test  as of timestamp to_timestamp('2011-10-25 13:45:00','yyyy-mm-dd hh2 ...

  7. 【Java知识点专项练习】之 接口和抽象类的区别

    接口和抽象类的区别 接口(interface)可以说成是抽象类的一种特例,接口中的所有方法都必须是抽象的.接口中的方法定义默认为public abstract类型,接口中的成员变量类型默认为publi ...

  8. linux下删除大量小文件

    当目录下文件太多时,用rm删除文件会报错:-bash: /bin/rm: Argument list too long提示文件数目太多.解决的办法是使用如下命令:ls | xargs -n 10 rm ...

  9. DELPHI XE Android 开发笔记

    第一次编译时,设定android SDK: F:\RAD Studio XE6\PlatformSDKs\adt-bundle-windows-x86-20131030\sdk F:\RAD Stud ...

  10. C++构造函数、new、delete

    1. c++在调用构造函数时,才会把最开始的虚表指针指向虚表. 2.在构造函数或者析构函数中调用虚函数. 编译上没有问题. 运行时,调用虚函数不会发生多态行为,会调用正在构造的类的虚函数. 详细可见c ...