python 爬取猫眼电影top100数据
最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel。
- 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据
- 使用语言:python
- 工具:PyCharm
- 涉及库:requests、re、openpyxl(高版本excel操作库)
实现代码
# -*- coding: utf-8 -*-
# @Author : yocichen
# @Email : yocichen@126.com
# @File : maoyan100.py
# @Software: PyCharm
# @Time : 2019
# @UpdateTime : 2020/4/26 import requests
from requests import RequestException
import re
import openpyxl
import traceback # Get page's html by requests module
def get_one_page(url):
try:
headers = {
'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'
}
# Sometimes, the proxies need to be replaced.
# You can get them by accessing https://www.kuaidaili.com/free/inha/
proxies = {
'http': '60.190.250.120:8080'
}
# use headers to avoid 403 Forbidden Error(reject spider)
response = requests.get(url, headers=headers, proxies=proxies)
if response.status_code == 200 :
return response.text
return None
except RequestException:
traceback.print_exc()
return None # Get useful info from html of a page by re module
def parse_one_page(html):
try:
pattern = re.compile('<dd>.*?board-index.*?>(\d+)<.*?<a.*?title="(.*?)"'
+'.*?data-src="(.*?)".*?</a>.*?star">[\\s]*(.*?)[\\n][\\s]*</p>.*?'
+'releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?'
+'fraction">(.*?)</i>.*?</dd>', re.S)
items = re.findall(pattern, html)
return items
except Exception:
traceback.print_exc()
return [] # Main call function
def main(url):
page_html = get_one_page(url)
parse_res = parse_one_page(page_html)
return parse_res # Write the useful info in excel(*.xlsx file)
def write_excel_xlsx(items):
wb = openpyxl.Workbook()
ws = wb.active
rows = len(items)
cols = len(items[0])
# First, write col's title.
ws.cell(1, 1).value = '编号'
ws.cell(1, 2).value = '片名'
ws.cell(1, 3).value = '宣传图片'
ws.cell(1, 4).value = '主演'
ws.cell(1, 5).value = '上映时间'
ws.cell(1, 6).value = '评分'
# Write film's info
for i in range(0, rows):
for j in range(0, cols):
if j != 5:
ws.cell(i+2, j+1).value = items[i][j]
else:
ws.cell(i+2, j+1).value = items[i][j]+items[i][j+1]
break
# Save the work book as *.xlsx
wb.save('maoyan_top100.xlsx') if __name__ == '__main__':
print('spider working...')
res = []
url = 'https://maoyan.com/board/4?'
for i in range(0, 10):
if i == 0:
res = main(url)
else:
newUrl = url+'offset='+str(i*10)
res.extend(main(newUrl))
print('writing into excel...')
write_excel_xlsx(res)
print('work done!\nNote: the data is in the current directory.')
更新效果图:
后记
入门了一点后发现,如果使用正则表达式和requests库来实行进行数据爬取的话,分析HTML页面结构和正则表达式的构造是关键,剩下的工作不过是替换url罢了。
补充一个分析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)-----python爬取猫眼电影top100榜
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...
- python爬取猫眼电影top100
最近想研究下python爬虫,于是就找了些练习项目试试手,熟悉一下,猫眼电影可能就是那种最简单的了. 1 看下猫眼电影的top100页面 分了10页,url为:https://maoyan.com/b ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天,恋习Python的手把手系列,手把手教你入门Python爬虫,爬取猫眼电影TOP100榜信息,将涉及到基础爬虫 ...
- 40行代码爬取猫眼电影TOP100榜所有信息
主要内容: 一.基础爬虫框架的三大模块 二.完整代码解析及效果展示 1️⃣ 基础爬虫框架的三大模块 1.HTML下载器:利用requests模块下载HTML网页. 2.HTML解析器:利用re正则表 ...
- # [爬虫Demo] pyquery+csv爬取猫眼电影top100
目录 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 代码君 [爬虫Demo] pyquery+csv爬取猫眼电影top100 站点分析 https://maoyan.co ...
- 用requests库爬取猫眼电影Top100
这里需要注意一下,在爬取猫眼电影Top100时,网站设置了反爬虫机制,因此需要在requests库的get方法中添加headers,伪装成浏览器进行爬取 import requests from re ...
- Python爬虫项目--爬取猫眼电影Top100榜
本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构, ...
- python应用-爬取猫眼电影top100
import requests import re import json import time from requests.exceptions import RequestException d ...
随机推荐
- iOS 设备数据管理工具 iMazing v2.10.3 绿色便携版
iMazing 是一款可以帮助用户管理 iOS 设备的软件,功能远远超出 iTunes.iMazing 连接你的 iOS 设备(iPhone. iPad 或 iPod)相连,使用起来也非常的方便.你可 ...
- 02-15 Logistic回归(鸢尾花分类)
目录 Logistic回归(鸢尾花分类) 一.导入模块 二.获取数据 三.构建决策边界 四.训练模型 4.1 C参数与权重系数的关系 五.可视化 更新.更全的<机器学习>的更新网站,更有p ...
- 03-01 K-Means聚类算法
目录 K-Means聚类算法 一.K-Means聚类算法学习目标 二.K-Means聚类算法详解 2.1 K-Means聚类算法原理 2.2 K-Means聚类算法和KNN 三.传统的K-Means聚 ...
- Django-debug-toolbar(调试使用)
Django-debug-toolbar django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. https: ...
- 实验吧之【Forms、天网管理系统】
Forms 原题链接 http://ctf5.shiyanbar.com/10/main.php Form 其实是个提示,代表html表单 F12 查看源码,发现 <input name=&qu ...
- [TYVJ2340] 送礼物 - 双向搜索
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 作为惩罚,GY被遣送去帮助某神牛给女生送礼物(GY:貌似是个好差事)但是在GY看到 ...
- 控制反转和依赖注入(对IOC,DI理解+案例)
理解 控制反转说的官方一点就是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其实就是一种设计思想,大概思想就是把设计好的对象交给容器控制,而不是在你内部直接控制. 依赖注入是控制反 ...
- vue-cli3 搭建 vue 项目
vue-cli3 搭建 vue 项目 项目是在mac的环境下配置的 win的同学请移步[https://www.cnblogs.com/zhaomeizi/p/8483597.html] 安装 nod ...
- C#操作sql server
C#操作sqlserver跟操作其他数据没有太大差别,但是又一些细节要注意一下. 在安装sqlserver时不要选择默认实例,如果是则需要更改设置,还有远程连接要去连接服务中设置一下,如端口1433等 ...
- django-表单之模型表单(三)
models.py-->forms.py-->views.py(get)--index.html-->views.py(post)-->home.html urls.py fr ...