要求

提取出猫眼电影的top100的电影名称、时间、评分图片等信息,提取的URL链接为:https://maoyan.com/board/4
获取的信息需要以文件的形式保存下来

思路

流程图如下所示:

import requests
# 定义一个可用来爬取HTML的链接
def get_one_page(url):headers = {'User-Agent':'Mozilla/4.0(compatible; MSIE 5.5; Windows NT)'}response=requests.get(url=url,headers=headers)if response.status_code == 200 :html=response.textelse:return Nonedef main():url = 'https://maoyan.com/board/4'get_one_page(url)main()

这里第一步首先实现了对网页数据的抓取,但是还有几个问题:

  1. 抓取的数据没有经过正则解析,仅仅知识网页的源数据
  2. 程序要实现的目标是提取前一百个电影的数据,但是这里仅仅只能获取前十
    针对上面的问题:
  3. 定义一个函数对源代码进行解析
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CGPf46zQ-1615448425993)(attachment:3abe6ab6-071e-40c5-851b-66990508b0fa.png)]

从以上链可以看到每一部电影的具体信息都在一个dd之内保存,明确目标,要获取的内容有index序号、图片链接、电影名、主演、上映时间。定义一个函数对信息进行解析。

import requests
# 定义一个可用来爬取HTML的链接
def get_one_page(url):headers = {'User-Agent':'Mozilla/4.0(compatible; MSIE 5.5; Windows NT)'}response=requests.get(url=url,headers=headers)if response.status_code == 200 :html=response.textelse:return Nonedef parse_data(html):patern=re.compile('<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?<a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>',re.S)item = 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]}itemdef main():url = 'https://maoyan.com/board/4'html=get_one_page(url)parse_data(html)main()

这里再解析网页内容的时候,用到了前面的正则解析式,从这次项目中,明晰了以下几点内容:

  1. 对于使用括号获取的内容,前后的具体指向性符号一定要完整,以保证获取信息的准确性
  2. 对于任何其他符号,只要是不在目标内的,应该全部用.*?的方式略过
  3. 建议使用compile方法,储存每次的正则表达式

解析之后的内容通过yield方式输出成为字典形状,补充一个关于yield函数的知识点:

  1. yield函数是一个函数内部的generator,这个方法必须在函数内部才能调用
  2. https://blog.csdn.net/poyue8754/article/details/84680609

同一份正则解析式,把答案抄上来都用不了

成功打开后的数据:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zf6UL0yy-1615448425995)(attachment:64cc9cf8-4fd6-4e3b-b0dc-4c2e00e67c38.png)]
可以看到数据其实是比较乱的,最好能通过字典进行规整,可以通过循环设置

通过以上的方法已经实现了,网页内容的爬取和解析,接下来要将这部分内容给存储下来

import jsondef write_to_file(content):with open('result.txt','a',encoding='utf-8') as f:print(type(json.dumps(content)))f.write(json.dumps(content,ensure_ascii=False)+'\n')

这里通过调用了json库的dumps()方法实现词典的序列化,同时指定ensure_ascii=False,可以保证输出的内容是中文格式而不是Unicode编码

import time
import requests
import json
from requests.exceptions import RequestException
import re# 定义一个可用来爬取HTML的链接
def get_one_page(url):try:headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}response = requests.get(url, headers=headers)if response.status_code == 200:return response.textreturn Noneexcept RequestException:return Nonedef 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]}def write_to_file(content):with open('result.txt','a',encoding='utf-8') as f:print(type(json.dumps(content)))f.write(json.dumps(content,ensure_ascii=False)+'\n')def main(offset):url = 'http://maoyan.com/board/4?offset=' + str(offset)html = get_one_page(url)for item in parse_one_page(html):print(item)write_to_file(item)if __name__ == '__main__':for i in range(10):main(offset=i * 10)time.sleep(1)
{'index': '1', 'image': 'https://p0.meituan.net/movie/414176cfa3fea8bed9b579e9f42766b9686649.jpg@160w_220h_1e_1c', 'title': '我不是药神', 'actor': '徐峥,周一围,王传君', 'time': '2018-07-05', 'score': '9.6'}
<class 'str'>
{'index': '2', 'image': 'https://p0.meituan.net/movie/8112a8345d7f1d807d026282f2371008602126.jpg@160w_220h_1e_1c', 'title': '肖申克的救赎', 'actor': '蒂姆·罗宾斯,摩根·弗里曼,鲍勃·冈顿', 'time': '1994-09-10(加拿大)', 'score': '9.5'}
<class 'str'>
{'index': '3', 'image': 'https://p1.meituan.net/movie/c9b280de01549fcb71913edec05880585769972.jpg@160w_220h_1e_1c', 'title': '绿皮书', 'actor': '维果·莫腾森,马赫沙拉·阿里,琳达·卡德里尼', 'time': '2019-03-01', 'score': '9.5'}
<class 'str'>
{'index': '4', 'image': 'https://p0.meituan.net/movie/609e45bd40346eb8b927381be8fb27a61760914.jpg@160w_220h_1e_1c', 'title': '海上钢琴师', 'actor': '蒂姆·罗斯,比尔·努恩,克兰伦斯·威廉姆斯三世', 'time': '2019-11-15', 'score': '9.3'}
<class 'str'>
{'index': '5', 'image': 'https://p1.meituan.net/movie/ac8f0004928fbce5a038a007b7c73cec746794.jpg@160w_220h_1e_1c', 'title': '小偷家族', 'actor': '中川雅也,安藤樱,松冈茉优', 'time': '2018-08-03', 'score': '8.1'}
<class 'str'>
{'index': '6', 'image': 'https://p0.meituan.net/movie/61fea77024f83b3700603f6af93bf690585789.jpg@160w_220h_1e_1c', 'title': '霸王别姬', 'actor': '张国荣,张丰毅,巩俐', 'time': '1993-07-26', 'score': '9.5'}
<class 'str'>
{'index': '7', 'image': 'https://p0.meituan.net/movie/005955214d5b3e50c910d7a511b0cb571445301.jpg@160w_220h_1e_1c', 'title': '哪吒之魔童降世', 'actor': '吕艳婷,囧森瑟夫,瀚墨', 'time': '2019-07-26', 'score': '9.6'}
<class 'str'>
{'index': '8', 'image': 'https://p1.meituan.net/movie/580d81a2c78bf204f45323ddb4244b6c6821175.jpg@160w_220h_1e_1c', 'title': '美丽人生', 'actor': '罗伯托·贝尼尼,朱斯蒂诺·杜拉诺,赛尔乔·比尼·布斯特里克', 'time': '2020-01-03', 'score': '9.3'}
<class 'str'>
{'index': '9', 'image': 'https://p1.meituan.net/movie/6bea9af4524dfbd0b668eaa7e187c3df767253.jpg@160w_220h_1e_1c', 'title': '这个杀手不太冷', 'actor': '让·雷诺,加里·奥德曼,娜塔莉·波特曼', 'time': '1994-09-14(法国)', 'score': '9.4'}
<class 'str'>
{'index': '10', 'image': 'https://p0.meituan.net/moviemachine/c2496a7290a72eac6081321898c347693550574.jpg@160w_220h_1e_1c', 'title': '盗梦空间', 'actor': '莱昂纳多·迪卡普里奥,渡边谦,约瑟夫·高登-莱维特', 'time': '2010-09-01', 'score': '9.0'}
<class 'str'>
{'index': '11', 'image': 'https://p0.meituan.net/movie/15f1ac49b6d1ff7b71207672993ed6901536456.jpg@160w_220h_1e_1c', 'title': '怦然心动', 'actor': '玛德琳·卡罗尔,卡兰·麦克奥利菲,艾丹•奎因', 'time': '2010-07-26(美国)', 'score': '8.9'}
<class 'str'>
{'index': '12', 'image': 'https://p0.meituan.net/movie/30b20139e68c46d02e0893277d633b701292458.jpg@160w_220h_1e_1c', 'title': '千与千寻', 'actor': '柊瑠美,周冬雨,入野自由', 'time': '2019-06-21', 'score': '9.3'}
<class 'str'>
{'index': '13', 'image': 'https://p0.meituan.net/movie/b41795c4a88479137e40ebdc3d7dc040238291.jpg@160w_220h_1e_1c', 'title': '阿甘正传', 'actor': '汤姆·汉克斯,罗宾·怀特,加里·西尼斯', 'time': '1994-07-06(美国)', 'score': '9.4'}
<class 'str'>
{'index': '14', 'image': 'https://p0.meituan.net/moviemachine/eb5e33480a3d3b0f813673a33eb556381148211.jpg@160w_220h_1e_1c', 'title': '触不可及', 'actor': '弗朗索瓦·克鲁塞,奥玛·希,安娜·勒尼', 'time': '2011-11-02(法国)', 'score': '9.1'}
<class 'str'>
{'index': '15', 'image': 'https://p0.meituan.net/moviemachine/94138dc0290a74b35786b6e3b8ece8607940668.jpg@160w_220h_1e_1c', 'title': '星际穿越', 'actor': '马修·麦康纳,安妮·海瑟薇,杰西卡·查斯坦', 'time': '2014-11-12', 'score': '9.3'}
<class 'str'>
{'index': '16', 'image': 'https://p0.meituan.net/movie/59c05effc4755c627901fb7e044d071d851055.jpg@160w_220h_1e_1c', 'title': '楚门的世界', 'actor': '金·凯瑞,劳拉·琳妮,诺亚·艾默里奇', 'time': '1998(罗马尼亚)', 'score': '8.9'}
<class 'str'>
{'index': '17', 'image': 'https://p0.meituan.net/movie/b0d986a8bf89278afbb19f6abaef70f31206570.jpg@160w_220h_1e_1c', 'title': '辛德勒的名单', 'actor': '连姆·尼森,拉尔夫·费因斯,本·金斯利', 'time': '1993-11-30(美国)', 'score': '9.2'}
<class 'str'>
{'index': '18', 'image': 'https://p0.meituan.net/movie/aeb864fa21d578d845b9cefc056e40cb2874891.jpg@160w_220h_1e_1c', 'title': '摔跤吧!爸爸', 'actor': '阿米尔·汗,沙克希·坦沃,法缇玛·萨那·纱卡', 'time': '2017-05-05', 'score': '9.8'}
<class 'str'>
{'index': '19', 'image': 'https://p0.meituan.net/movie/f433a705dd26d96e95ba6582ddccf5ad1159253.jpg@160w_220h_1e_1c', 'title': '寻梦环游记', 'actor': '安东尼·冈萨雷斯,本杰明·布拉特,盖尔·加西亚·贝纳尔', 'time': '2017-11-24', 'score': '9.6'}
<class 'str'>
{'index': '20', 'image': 'https://p0.meituan.net/movie/34998e31c6d07475f1add6b8b16fd21d192579.jpg@160w_220h_1e_1c', 'title': '少年派的奇幻漂流', 'actor': '苏拉·沙玛,伊尔凡·可汗,塔布', 'time': '2012-11-22', 'score': '9.0'}
<class 'str'>
{'index': '21', 'image': 'https://p1.meituan.net/movie/d28b729ffe72353a72d1e7ef8a9b90591544978.jpg@160w_220h_1e_1c', 'title': '何以为家', 'actor': '赞恩·阿尔·拉菲亚,约丹诺斯·希费罗,博鲁瓦蒂夫·特雷杰·班科尔', 'time': '2019-04-29', 'score': '9.3'}
<class 'str'>
{'index': '22', 'image': 'https://p0.meituan.net/moviemachine/7707dc3478f9ab26871688f4964ae45d6672925.jpg@160w_220h_1e_1c', 'title': '小丑', 'actor': '华金·菲尼克斯,罗伯特·德尼罗,亚历克·鲍德温', 'time': '2019-10-04(美国)', 'score': '8.6'}
<class 'str'>
{'index': '23', 'image': 'https://p0.meituan.net/movie/04263651e43c11ce538b180988ff0a5e231189.jpg@160w_220h_1e_1c', 'title': '忠犬八公的故事', 'actor': 'Forest,理查·基尔,琼·艾伦', 'time': '2009-06-13(美国)', 'score': '9.3'}
<class 'str'>
{'index': '24', 'image': 'https://p1.meituan.net/movie/4e6e22ea788a72b1cf769bd5537bd38d37701.jpg@160w_220h_1e_1c', 'title': '情书', 'actor': '中山美穗,丰川悦司,范文雀', 'time': '1999-03', 'score': '8.7'}
<class 'str'>
{'index': '25', 'image': 'https://p1.meituan.net/movie/b607fba7513e7f15eab170aac1e1400d878112.jpg@160w_220h_1e_1c', 'title': '泰坦尼克号', 'actor': '莱昂纳多·迪卡普里奥,凯特·温丝莱特,比利·赞恩', 'time': '1998-04-03', 'score': '9.4'}
<class 'str'>
{'index': '26', 'image': 'https://p0.meituan.net/moviemachine/e5daa8748733820faab91102bd0bc4507730353.jpg@160w_220h_1e_1c', 'title': '当幸福来敲门', 'actor': '威尔·史密斯,贾登·史密斯,坦迪·牛顿', 'time': '2008-01-17', 'score': '9.3'}
<class 'str'>
{'index': '27', 'image': 'https://p1.meituan.net/movie/ca4a128a5a54d5b5e35ceba622636c831810197.jpg@160w_220h_1e_1c', 'title': '三傻大闹宝莱坞', 'actor': '阿米尔·汗,黄渤,卡琳娜·卡普', 'time': '2011-12-08', 'score': '9.1'}
<class 'str'>
{'index': '28', 'image': 'https://p0.meituan.net/movie/c304c687e287c7c2f9e22cf78257872d277201.jpg@160w_220h_1e_1c', 'title': '龙猫', 'actor': '秦岚,糸井重里,岛本须美', 'time': '2018-12-14', 'score': '9.2'}
<class 'str'>
{'index': '29', 'image': 'https://p0.meituan.net/movie/70de97ebb6b5251ecb7c3f6d7a782a7f189340.jpg@160w_220h_1e_1c', 'title': '放牛班的春天', 'actor': '热拉尔·朱尼奥,让-巴蒂斯特·莫尼耶,玛丽·布奈尔', 'time': '2004-10-16', 'score': '8.8'}
<class 'str'>
{'index': '30', 'image': 'https://p1.meituan.net/movie/2a0783b4fd95566568f24adfad2181bb5392280.jpg@160w_220h_1e_1c', 'title': '熔炉', 'actor': '孔刘,郑有美,金智英', 'time': '2011-09-22(韩国)', 'score': '8.8'}
<class 'str'>

总结

  1. 在很多方面的知识储备不足
  2. 错误捕捉自己以前没弄过,以后要经常来
  3. 打错单词是比较不应该的
  4. 对正则表达式应该仔细校对

爬取猫眼电影排行——第一版相关推荐

  1. 【崔庆才教材】《Python3网络爬虫开发实战》3.4爬取猫眼电影排行代码更正(绕过美团验证码)

    前情提要 最近有七八个小伙伴私信问我说崔大神的<Python3网络爬虫开发实战>教材中 "3.4爬取猫眼电影排行" 一节的代码目前不能用. 首先附上崔大神的github ...

  2. Python3爬虫——爬取猫眼电影排行

    一.爬取猫眼电影排行 目标:提取猫眼电影TOP100的电影排名.名称.主演.上映时间.评分.海报链接. URL:https://maoyan.com/board/4 保存:JSON # 版本:Pyth ...

  3. Python实战之如何爬取猫眼电影排行?本文详解

    本节中,我们利用requests库和正则表达式来抓取猫眼电影TOP100的相关内容.requests比urllib使用更加方便,而且目前我们还没有系统学习HTML解析库,所以这里就选用正则表达式来作为 ...

  4. python学习实践--爬取猫眼电影排行

    爬取前一定要注意观察网站的url地址,如果是分页的话,找出每一页不同的地址规律,同时,尽量在network选项卡部分查看网页源代码,否则源码可能经过JavaScript操作与原始请求不同. 抓取站点 ...

  5. 爬取猫眼电影排行版TOP100

    初入爬虫,自己确实对爬虫挺感兴趣,觉得爬到数据的那种感觉很开心. 有两点比较难,一是评分,二是票房,评分可以在这个地方取到, 票房采用的是自定义字体,难度较大,需要将其每个点的坐标下载下来对比–未解决 ...

  6. 00_抓取猫眼电影排行TOP100

    前言: 学习python3爬虫有一段时间了,熟悉了爬虫的一些基本原理和基本库的使用,本次就准备利用requests库和正则表达式来抓取猫眼电影排行TOP100的相关内容. 1.本次目标: 爬取猫眼电影 ...

  7. 使用Requests爬取猫眼电影

    谈到爬虫大家可能对Requests库不会陌生,相比urllib库的话Requests库在处理网页认证和Cookies时更加方便,话不多少,下面让我通过爬取猫眼电影前100名排行榜实例来说明一下. 首先 ...

  8. python战反爬虫:爬取猫眼电影数据 (一)(Requests, BeautifulSoup, MySQLdb,re等库)

    姓名:隋顺意 博客:Sui_da_xia 微信名:世界上的霸主 本篇文章未涉及猫眼反爬,主要介绍爬取无反爬内容,战反爬内容请去 python战反爬虫:爬取猫眼电影数据 (二)(Requests, Be ...

  9. python3网络爬虫开发实战-抓取猫眼电影排行(正则表达式版)

    抓取猫眼电影排行 目前没有学习HTML解析库,这里先选用正则表达式作为解析工具 目标 提取出猫眼电影top100的电影名称.时间.评分.图片等信息.提取的站点URL为http://maoyan.com ...

最新文章

  1. R语言广义线性模型函数GLM、glm函数构建泊松回归模型(Poisson regression)、输出提供偏差(deviances)、回归参数和标准误差、以及系数的显著性p值
  2. weblogic管理1——创建 和 删除一个domain
  3. python字典popitem和pop_python 字典之pop() and popitem()
  4. UIAlertController 简单修改title以及按钮的字体颜色
  5. java学习类的笔记
  6. 百万个小油馕跨越3000公里来支援!西安加油!
  7. JavaScript之 函数
  8. mysql sample函数_SAMPLE函数
  9. 【amp;#9733;】SPF(Dijkstra)算法完美教程
  10. 降维系列之 SNE与t-SNE
  11. 阿里巴巴Java开发手册(泰山版)
  12. 用Java写一个小游戏
  13. ntp和ptp实现_NTP/PTP时间同步入门
  14. HTML入门笔记13-HTML中font标签
  15. 银行卡卡号归属地汇总
  16. PaddleHub创意作品 -无价之姐之三十而已
  17. 电脑分区后,电脑提醒没有权限在此位置保存文件怎么办?
  18. Spring Destroying singletons ... root of factory hierarchy 问题【已解决】
  19. 根据userAgent获取浏览器/操作系统/设备类型等信息
  20. 离谱:火狐浏览器重启后插件,书签,浏览记录全部没了???

热门文章

  1. 细节纹理和锐化测试(枯叶图)—imatest
  2. oracle 分区表空间迁移,oracle分区表的迁移
  3. 火狐便携版Firefoxcn-v55.0分享 支持插件定制化
  4. 代码质量管理平台SonarQube安装使用Centos7 三 Maven处理提交至远程仓库
  5. 实现手机安装根证书(系统证书)和使用drony 解决普通代理部分应用不能抓包的问题
  6. 一步一步学区块链(5)智能合约
  7. 7-10 节约有理 (25 分) c++结构体排序
  8. 【LeetCode】Day119-找到小镇的法官可达所有点的最少点数目
  9. 课堂管理系统——Android
  10. 灰光和彩光_5G承载网到底有什么不同?