python反反爬,爬取猫眼评分.
解决网站爬取时,内容类似:$#x12E0;样式,且每次字体文件变化。
下载FontCreator

.
用FontCreator打开base.woff.查看对应字体关系

初始化时将对应关系写入字典中。


 #!/usr/bin/env python
# coding:utf-8
# __author__ = "南楼" import requests
import re
import os from fontTools.ttLib import TTFont #下载字体
class MaoYan(object): def __init__(self):
self.url = 'http://maoyan.com/films/1198214'
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
}
self.base_num = {} # 编号—数字
self.base_obj = {} # 编号—对象
# base.woff 为当前网站下载的一个字体
self.base_font_file = TTFont('./fonts/base.woff')
# 需要先下载字体编辑软件(FontCreator),以便查看对应关系
self.base_num["uniF3BA"] = ""
self.base_num["uniF2A9"] = ""
self.base_num["uniE6A5"] = ""
self.base_num["uniF680"] = ""
self.base_num["uniE69C"] = ""
self.base_num["uniE710"] = ""
self.base_num["uniE07D"] = ""
self.base_num["uniE5A7"] = ""
self.base_num["uniEC7A"] = ""
self.base_num["uniE2A3"] = "" for key in self.base_num:
self.base_obj[key] =self.base_font_file['glyf'][key] def baseobj(self):
for key in self.base_num: self.base_obj[key] =self.base_font_file['glyf'][key] # 获得woff内编号对应的字体对象
return self.base_obj # 发送请求获得响应
def get_html(self, url):
response = requests.get(url, headers=self.headers)
return response.content def create_font(self, re_font_file):
# 列出已下载文件
file_list = os.listdir('./fonts')
# 判断是否已下载
if re_font_file not in file_list: print('不在字体库中, 下载:', re_font_file)
url = 'http://vfile.meituan.net/colorstone/' + re_font_file
new_file = self.get_html(url)
with open('./fonts/' + re_font_file, 'wb') as f:
f.write(new_file) # 打开字体文件,创建 self.font_file属性
self.font_file = TTFont('./fonts/' + re_font_file) def get_num_from_font_file(self, re_star): newstar = re_star.upper().replace("&#X", "uni")
realnum = newstar.replace(";", "")
numlist = realnum.split(".")
# gly_list = self.font_file.getGlyphOrder() #uni列表['glyph00000', 'x', 'uniF680', 'uniE2A3', 'uniE710', 'uniE69C', 'uniEC7A', 'uniF2A9', 'uniE5A7', 'uniE07D', 'uniE6A5', 'uniF3BA']
star_rating = []
for hax_num in numlist:
font_file_num = self.font_file['glyf'][hax_num]
for key in self.baseobj():
if font_file_num == self.base_obj[key]:
star_rating.append(self.base_num[key])
# 星级评分待优化,暂不支持10.0,
star_rating = star_rating[0]+"."+star_rating[1]
return star_rating def start_crawl(self):
html = self.get_html(self.url).decode('utf-8') # 正则匹配字体文件
re_font_file = re.findall(r'vfile\.meituan\.net\/colorstone\/(\w+\.woff)', html)[0]
self.create_font(re_font_file)
# 正则匹配星级评分
re_star_rating = re.findall(r'<span class="index-left info-num ">\s+<span class="stonefont">(.*?)</span>\s+</span>', html)[0]
star_rating = self.get_num_from_font_file(re_star_rating)
print("星级评分:", star_rating) if __name__ == '__main__': m = MaoYan()
m.start_crawl()

python反反爬,爬取猫眼评分的更多相关文章

  1. python+requests+re匹配抓取猫眼上映电影信息

    python+requests抓取猫眼中上映电影,re正则匹配获取对应电影的排名,图片地址,片名,主演及上映时间和评分 import requests import re, json def get_ ...

  2. Python爬虫实例:爬取猫眼电影——破解字体反爬

    字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...

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

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

  4. python 爬取猫眼电影top100数据

    最近有爬虫相关的需求,所以上B站找了个视频(链接在文末)看了一下,做了一个小程序出来,大体上没有修改,只是在最后的存储上,由txt换成了excel. 简要需求:爬虫爬取 猫眼电影TOP100榜单 数据 ...

  5. 【Python必学】Python爬虫反爬策略你肯定不会吧?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 正文 Python爬虫反爬策略三部曲,拥有这三步曲就可以在爬虫界立足了: ...

  6. python爬虫---详解爬虫分类,HTTP和HTTPS的区别,证书加密,反爬机制和反反爬策略,requests模块的使用,常见的问题

    python爬虫---详解爬虫分类,HTTP和HTTPS的区别,证书加密,反爬机制和反反爬策略,requests模块的使用,常见的问题 一丶爬虫概述       通过编写程序'模拟浏览器'上网,然后通 ...

  7. 票房和口碑称霸国庆档,用 Python 爬取猫眼评论区看看电影《我和我的家乡》到底有多牛

    今年的国庆档电影市场的表现还是比较强势的,两名主力<我和我的家乡>和<姜子牙>起到了很好的带头作用. <姜子牙>首日破 2 亿,一举刷新由<哪吒之魔童降世&g ...

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

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

  9. Python 爬取 猫眼 top100 电影例子

    一个Python 爬取猫眼top100的小栗子 import json import requests import re from multiprocessing import Pool #//进程 ...

随机推荐

  1. ZAB协议与Paxos算法

    ZooKeeper并没有直接采用Paxos算法,而是采用一种被称为ZAB(ZooKeeper Atomic Broadcast)的一致性协议 ZooKeeper是一个典型的分布式数据一致性的解决方案, ...

  2. centos+git+gitolite 安装和部署

    一.部署环境 系统:CentOS 6.4x64 最小化安装 IP:192.168.52.131 git默认使用SSH协议,在服务器上基本上不用怎么配置就能直接使用.但是如果面向团队服务,需要控制权限的 ...

  3. 问题:CGI返回给前端的汉字数据是乱码(已解决)

    今天,我在写CGI程序,把后台数据返回到前台页面上,出现乱码, 不好看,所以,我在Content-type:application后面加了字符集的设置(字符集和前端一样), fprintf(stdou ...

  4. Hibernate-day03

    双向的many2one/one2many 双向many2one/one2many的问题:一定有额外的SQL发出;在数据库里面,外键表示many2one或者one2many的关系,外键是没有方向的;但是 ...

  5. Linux下Python模式下【Tab】自动补全

    注:此文为转载他人博客,如有侵权,请联系我删除 1.我们需要一个tab补全的功能脚本  #!/usr/bin/python # python tab file import sys import re ...

  6. Random类 一般跟生成随机数有关

    public class MyRandom extends Random{ public static void main(String[] args) { // 随机数,生产随机数 // java提 ...

  7. 理解es6 中 arrow function的this

    箭头函数相当于定义时候,普通函数.bind(this)箭头函数根本没有自己的this,导致内部的this就是定义时候的外层代码块中的this.外层代码块中的this,则取决于执行时候环境上下文cont ...

  8. 中国省份毗邻关系JSON数据[相邻省份][所辖市级信息][行政区划]

    最近做一个需求, 需要一份每个省份相邻[毗邻]的省份信息,这里整理了一版. json 数据,结构大致这样子的. [ { "id": 7, "name": &qu ...

  9. 平安银行在开源技术选型上的思考和实践 RocketMQ

    小结: 1. https://mp.weixin.qq.com/s/z_c5D8fvHaYvHSczm0nYFA 平安银行在开源技术选型上的思考和实践 平安银行·吴建峰 阿里巴巴中间件 3月7日 随着 ...

  10. IDEA导入Git项目后右键项目找不到Git选项的解决方法

    进入Setting -> version Control -> 在下图的第二步中是否有灰色的Module,选中它->点击第三步的+即可 转自:https://blog.csdn.ne ...