爬虫项目爬取猫眼电影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. ios开发之--tableview刷新某一个区和某一行

    在开发中,有时候,我们不需要刷新整个表,只需要刷新局部数据即可,具体代码如下: //section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWith ...

  2. __stdcall __cdecl 引起的程序崩溃

    崩溃弹出的截图如下 看到0xC0000005, 访问冲突的问题, 九成九是内存访问违规, 比如访问了已经释放的指针, 又或者是离开函数时栈被破坏之类. 找了一下午一直没有头绪, 好在有一份可以执行的源 ...

  3. iOS Ad hoc

    There's one situation in which you need an Ad Hoc profile, and that's when you want to test Push Not ...

  4. 找不同diff-打补丁patch

    Q:为什么要找不同,为什么要打补丁? A: 在Linux应用中,作为DBA,我们知道MySQL跑在Linux系统之上,数据库最重要的追求就是性能,“稳”是重中之重,所以不能动不动就是换系统或是换这换那 ...

  5. CentOS安装、配置RabbitMQ

    安装步骤(rpm包安装): rpm安装官网:http://www.rabbitmq.com/install-rpm.html 下载rpm并安装: CentOs: wget http://www.rab ...

  6. /var/spool/postfix/maildrop/ 中有大量的文件

    今天查看硬盘剩余的容量,发现‘/’目录下占用了大量的空间:可我在这个目录下面没有放什么东西:仔细查看在/var/spool/postfix/maildrop/ 中发现了大量的文件.怎么会有这么多的文件 ...

  7. PHP curl登录 跳过验证码

    <?php switch($_GET['do']){ case 'vc': $cookieFile = "./test.tmp"; $url = 'http://localh ...

  8. JS使用中碰到的一些问题

    settimeout: 1.setTimeout(function () {//这个则会在1秒后进行弹出1 alert(1); }, 1000); 2.setTimeout(alert(1), 100 ...

  9. [转]java中判断字符串是否为数字的三种方法

    1用JAVA自带的函数public static boolean isNumeric(String str){  for (int i = str.length();--i>=0;){      ...

  10. java基础---->java注解的使用(一)

    注解是众多引入到Java SE5中的重要的语言变化之一.它为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便的使用这些数据.今天我们就开始学习一下java中注解的知识. j ...