最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel。

  • 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据
  • 使用语言:python
  • 工具:PyCharm
  • 涉及库:requests、re、openpyxl(高版本excel操作库)

实现代码

猫眼电影Robots

  1. # -*- coding: utf-8 -*-
  2. # @Author : yocichen
  3. # @Email : yocichen@126.com
  4. # @File : maoyan100.py
  5. # @Software: PyCharm
  6. # @Time : 2019
  7. # @UpdateTime : 2020/4/26
  8.  
  9. import requests
  10. from requests import RequestException
  11. import re
  12. import openpyxl
  13. import traceback
  14.  
  15. # Get page's html by requests module
  16. def get_one_page(url):
  17. try:
  18. headers = {
  19. 'user-agent': 'Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 53.0.2785.104Safari / 537.36Core / 1.53.4882.400QQBrowser / 9.7.13059.400'
  20. }
  21. # Sometimes, the proxies need to be replaced.
  22. # You can get them by accessing https://www.kuaidaili.com/free/inha/
  23. proxies = {
  24. 'http': '60.190.250.120:8080'
  25. }
  26. # use headers to avoid 403 Forbidden Error(reject spider)
  27. response = requests.get(url, headers=headers, proxies=proxies)
  28. if response.status_code == 200 :
  29. return response.text
  30. return None
  31. except RequestException:
  32. traceback.print_exc()
  33. return None
  34.  
  35. # Get useful info from html of a page by re module
  36. def parse_one_page(html):
  37. try:
  38. pattern = re.compile('<dd>.*?board-index.*?>(\d+)<.*?<a.*?title="(.*?)"'
  39. +'.*?data-src="(.*?)".*?</a>.*?star">[\\s]*(.*?)[\\n][\\s]*</p>.*?'
  40. +'releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?'
  41. +'fraction">(.*?)</i>.*?</dd>', re.S)
  42. items = re.findall(pattern, html)
  43. return items
  44. except Exception:
  45. traceback.print_exc()
  46. return []
  47.  
  48. # Main call function
  49. def main(url):
  50. page_html = get_one_page(url)
  51. parse_res = parse_one_page(page_html)
  52. return parse_res
  53.  
  54. # Write the useful info in excel(*.xlsx file)
  55. def write_excel_xlsx(items):
  56. wb = openpyxl.Workbook()
  57. ws = wb.active
  58. rows = len(items)
  59. cols = len(items[0])
  60. # First, write col's title.
  61. ws.cell(1, 1).value = '编号'
  62. ws.cell(1, 2).value = '片名'
  63. ws.cell(1, 3).value = '宣传图片'
  64. ws.cell(1, 4).value = '主演'
  65. ws.cell(1, 5).value = '上映时间'
  66. ws.cell(1, 6).value = '评分'
  67. # Write film's info
  68. for i in range(0, rows):
  69. for j in range(0, cols):
  70. if j != 5:
  71. ws.cell(i+2, j+1).value = items[i][j]
  72. else:
  73. ws.cell(i+2, j+1).value = items[i][j]+items[i][j+1]
  74. break
  75. # Save the work book as *.xlsx
  76. wb.save('maoyan_top100.xlsx')
  77.  
  78. if __name__ == '__main__':
  79. print('spider working...')
  80. res = []
  81. url = 'https://maoyan.com/board/4?'
  82. for i in range(0, 10):
  83. if i == 0:
  84. res = main(url)
  85. else:
  86. newUrl = url+'offset='+str(i*10)
  87. res.extend(main(newUrl))
  88. print('writing into excel...')
  89. write_excel_xlsx(res)
  90. print('work done!\nNote: the data is in the current directory.')

更新效果图:

后记

入门了一点后发现,如果使用正则表达式和requests库来实行进行数据爬取的话,分析HTML页面结构和正则表达式的构造是关键,剩下的工作不过是替换url罢了。

你可能需要的 GitHub 传送门


补充一个分析HTML构造正则的例子

猫眼经典科幻按照评价排序

审查元素我们会发现每一项都是<dd>****</dd>格式

我想要获取电影名称和评分,先拿出HTML代码看一看

试着构造正则

'.*?<dd>.*?movie-item-title.*?title="(.*?)">.*?integer">(.*?)<.*?fraction">(.*?)<.*?</dd>' (随手写的,未经验证)


参考资料

【B站视频 2018年最新Python3.6网络爬虫实战】https://www.bilibili.com/video/av19057145/?p=14

【猫眼电影robots】https://maoyan.com/robots.txt (最好爬之前去看一下,那些可爬那些不允许爬)

python 爬取猫眼电影top100数据的更多相关文章

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

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

  2. python爬取猫眼电影top100

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

  3. PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)

    利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...

  4. 50 行代码教你爬取猫眼电影 TOP100 榜所有信息

    对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...

  5. 40行代码爬取猫眼电影TOP100榜所有信息

    主要内容: 一.基础爬虫框架的三大模块 二.完整代码解析及效果展示 1️⃣  基础爬虫框架的三大模块 1.HTML下载器:利用requests模块下载HTML网页. 2.HTML解析器:利用re正则表 ...

  6. # [爬虫Demo] pyquery+csv爬取猫眼电影top100

    目录 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 代码君 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 https://maoyan.co ...

  7. 用requests库爬取猫眼电影Top100

    这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...

  8. Python爬虫项目--爬取猫眼电影Top100榜

    本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构,  ...

  9. python应用-爬取猫眼电影top100

    import requests import re import json import time from requests.exceptions import RequestException d ...

随机推荐

  1. 在我的新书里,尝试着用股票案例讲述Python爬虫大数据可视化等知识

    我的新书,<基于股票大数据分析的Python入门实战>,预计将于2019年底在清华出版社出版. 如果大家对大数据分析有兴趣,又想学习Python,这本书是一本不错的选择.从知识体系上来看, ...

  2. UVa12105 越大越好

    题文:https://vjudge.net/problem/12364(或者见紫书) 题解: 因为题目中有两个限制条件,那么我们就顺着题目的意思来dp,设dp[i][j]表示目前还剩下的i个火柴,用这 ...

  3. js继承机制的实现

    js继承机制的实现 1. 继承的概念 说明继承的最经典的例子:几何形状.实际上,几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形.矩形和五边形 ...

  4. 攻防世界(XCTF)逆向部分write up(一)

    晚上做几个简单的ctf逆向睡的更好 logmein elf文件 ida看看main函数伪代码 void __fastcall __noreturn main(__int64 a1, char **a2 ...

  5. [LUOGU1868] 饥饿的奶牛 - dp二分

    题目描述 有一条奶牛冲出了围栏,来到了一处圣地(对于奶牛来说),上面用牛语写着一段文字. 现用汉语翻译为: 有N个区间,每个区间x,y表示提供的x~y共y-x+1堆优质牧草.你可以选择任意区间但不能有 ...

  6. [Luogu3420][POI2005]SKA-Piggy Banks

    题目描述 Byteazar the Dragon has NNN piggy banks. Each piggy bank can either be opened with its correspo ...

  7. 非阻塞IO模型

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> # ...

  8. vue 父组件动态传值至子组件

    1.进行数据监听,数据每次变化就初始化一次子组件,进行调取达到传递动态数据的目的普通的监听: watch:{ data: function(newValue,oldValue){ doSomeThin ...

  9. PHP代码审计基础-初级篇

    对于php代码审计我也是从0开始学的,对学习过程进行整理输出沉淀如有不足欢迎提出共勉.对学习能力有较高要求,整个系列主要是在工作中快速精通php代码审计,整个学习周期5天 ,建议花一天时间熟悉php语 ...

  10. pycharm中常见错误提示

    1.类中定义函方法 PyCharm 提示Method xxx may be 'static': 原因:该方法不涉及对该类属性的操作,编译器建议声明为@staticmethod