如何爬取猫眼网电影票房数据

https://blog.csdn.net/qq_31032181/article/details/79153578

一、背景

 字体反爬应用还是很普遍。这两天有朋友咨询如何实现猫眼票房数据的爬取,这里其实与上面的文章核心思想是一致的,但是操作更复杂一些,本文做一个更详细的破解实践。

有对字体反爬还比较陌生的,请参考前文。

二、查找字体源

 猫眼电影是美团旗下的一家集媒体内容、在线购票、用户互动社交、电影衍生品销售等服务的一站式电影互联网平台。2015年6月,猫眼电影覆盖影院超过4000家,这些影院的票房贡献占比超过90%。目前,猫眼占网络购票70%的市场份额,每三张电影票就有一张出自猫眼电影,是影迷下载量较多、使用率较高的电影应用软件。同时,猫眼电影为合作影院和电影制片发行方提供覆盖海量电影消费者的精准营销方案,助力影片票房。

我们使用Chrome浏览页面,并查看源码,发现售票中涉及数字的,在页面显示正常,在源码中显示一段span包裹的不可见文本。

上面其实就是自定义字体搞的鬼。根据网页源码中,

<span class="stonefont">.</span>

使用了自定义的stonefont字体,我们在网页中查找stonefont,很快有了发现,这就是标准的@font-face定义方法。且每次访问,字体文件访问地址都会随机变化。

 我们访问其中woff文件的地址,可将woff字体文件下载到本地。前文中fonttools并不能直接解析woff字体,我们需要将woff字体转换成otf字体。百度可以直接转换字体 ,地址:http://fontstore.baidu.com/static/editor/index.html

三、字体解析

 otf就是我们常用的字体文件,可以使用系统自带的字体查看器查看,但是难以看到更多有效的信息,我们使用一个专用工具Font Creator查看。

可以看到,这个字体里有12个字(含一个空白字),每个字显示其字形和其字形编码。这里比之前字体解析更复杂的是,这里不仅字体编码每次都会变,字体顺序每次也会变,很难直接通过编码和顺序获取实际的数字。

因此,我们需要预先下载一个字体文件,人工识别其对应数值和字体,然后针对每次获取的新的字体文件,通过比对字体字形数据,得到其真实的数字值。

下面是使用fontTools.ttLib获取的单个字符的字形数据。

 
  1. <TTGlyph name="uniE183" xMin="0" yMin="-12" xMax="516" yMax="706">

  2. <contour>

  3. <pt x="134" y="195" on="1"/>

  4. <pt x="144" y="126" on="0"/>

  5. <pt x="217" y="60" on="0"/>

  6. <pt x="271" y="60" on="1"/>

  7. <pt x="335" y="60" on="0"/>

  8. <pt x="423" y="158" on="0"/>

  9. <pt x="423" y="311" on="0"/>

  10. <pt x="337" y="397" on="0"/>

  11. <pt x="270" y="397" on="1"/>

  12. <pt x="227" y="397" on="0"/>

  13. <pt x="160" y="359" on="0"/>

  14. <pt x="140" y="328" on="1"/>

  15. <pt x="57" y="338" on="1"/>

  16. <pt x="126" y="706" on="1"/>

  17. <pt x="482" y="706" on="1"/>

  18. <pt x="482" y="622" on="1"/>

  19. <pt x="197" y="622" on="1"/>

  20. <pt x="158" y="430" on="1"/>

  21. <pt x="190" y="452" on="0"/>

  22. <pt x="258" y="475" on="0"/>

  23. <pt x="293" y="475" on="1"/>

  24. <pt x="387" y="475" on="0"/>

  25. <pt x="516" y="346" on="0"/>

  26. <pt x="516" y="243" on="1"/>

  27. <pt x="516" y="147" on="0"/>

  28. <pt x="459" y="75" on="1"/>

  29. <pt x="390" y="-12" on="0"/>

  30. <pt x="271" y="-12" on="1"/>

  31. <pt x="173" y="-12" on="0"/>

  32. <pt x="112" y="42" on="1"/>

  33. <pt x="50" y="98" on="0"/>

  34. <pt x="42" y="188" on="1"/>

  35. </contour>

  36. <instructions/>

  37. </TTGlyph>

使用下面语句可以获取顺序的字符编码值,

 
  1. # 解析字体库font文件

  2. baseFont = TTFont('base.otf')

  3. maoyanFont = TTFont('maoyan.otf')

  4. uniList = maoyanFont['cmap'].tables[0].ttFont.getGlyphOrder()

  5. numList = []

  6. baseNumList = ['.', '3', '5', '1', '2', '7', '0', '6', '9', '8', '4']

  7. baseUniCode = ['x', 'uniE64B', 'uniE183', 'uniED06', 'uniE1AC', 'uniEA2D', 'uniEBF8',

  8. 'uniE831', 'uniF654', 'uniF25B', 'uniE3EB']

  9. for i in range(1, 12):

  10. maoyanGlyph = maoyanFont['glyf'][uniList[i]]

  11. for j in range(11):

  12. baseGlyph = baseFont['glyf'][baseUniCode[j]]

  13. if maoyanGlyph == baseGlyph:

  14. numList.append(baseNumList[j])

  15. break

四、内容替换

关键点攻破了,整个工作就好做了。先访问需要爬取的页面,获取字体文件的动态访问地址并下载字体,读取用户帖子文本内容,替换其中的自定义字体编码为实际文本编码,就可复原网页为页面所见内容了。

完整代码如下:

 
 
 
  1. # -*- coding:utf-8 -*-

  2. import requests

  3. from lxml import html

  4. import re

  5. import woff2otf

  6. from fontTools.ttLib import TTFont

  7. from bs4 import BeautifulSoup as bs

  8.  
  9. #抓取maoyan票房

  10. class MaoyanSpider:

  11. #页面初始化

  12. def __init__(self):

  13. self.headers = {

  14. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",

  15. "Accept-Encoding": "gzip, deflate, br",

  16. "Accept-Language": "zh-CN,zh;q=0.8",

  17. "Cache-Control": "max-age=0",

  18. "Connection": "keep-alive",

  19. "Upgrade-Insecure-Requests": "1",

  20. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",

  21. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"

  22. }

  23. # 获取票房

  24. def getNote(self):

  25. url = "http://maoyan.com/cinema/15887?poi=91871213"

  26. host = {'host':'maoyan.com',

  27. 'refer':'http://maoyan.com/news',}

  28. headers = dict(self.headers.items() + host.items())

  29. # 获取页面内容

  30. r = requests.get(url, headers=headers)

  31. #print r.text

  32. response = html.fromstring(r.text)

  33. u = r.text

  34. # 匹配ttf font

  35. cmp = re.compile(",\n url\('(//.*.woff)'\) format\('woff'\)")

  36. rst = cmp.findall(r.text)

  37. ttf = requests.get("http:" + rst[0], stream=True)

  38. with open("maoyan.woff", "wb") as pdf:

  39. for chunk in ttf.iter_content(chunk_size=1024):

  40. if chunk:

  41. pdf.write(chunk)

  42. # 转换woff字体为otf字体

  43. woff2otf.convert('maoyan.woff', 'maoyan.otf')

  44. # 解析字体库font文件

  45. baseFont = TTFont('base.otf')

  46. maoyanFont = TTFont('maoyan.otf')

  47. uniList = maoyanFont['cmap'].tables[0].ttFont.getGlyphOrder()

  48. numList = []

  49. baseNumList = ['.', '3', '5', '1', '2', '7', '0', '6', '9', '8', '4']

  50. baseUniCode = ['x', 'uniE64B', 'uniE183', 'uniED06', 'uniE1AC', 'uniEA2D', 'uniEBF8',

  51. 'uniE831', 'uniF654', 'uniF25B', 'uniE3EB']

  52. for i in range(1, 12):

  53. maoyanGlyph = maoyanFont['glyf'][uniList[i]]

  54. for j in range(11):

  55. baseGlyph = baseFont['glyf'][baseUniCode[j]]

  56. if maoyanGlyph == baseGlyph:

  57. numList.append(baseNumList[j])

  58. break

  59. uniList[1] = 'uni0078'

  60. utf8List = [eval("u'\u" + uni[3:] + "'").encode("utf-8") for uni in uniList[1:]]

  61. # 获取发帖内容

  62. soup = bs(u,"html.parser")

  63. index=soup.find_all('div', {'class': 'show-list'})

  64.  
  65. print '---------------Prices-----------------'

  66. for n in range(len(index)):

  67. mn=soup.find_all('h3', {'class': 'movie-name'})

  68. ting=soup.find_all('span', {'class': 'hall'})

  69. mt=soup.find_all('span', {'class': 'begin-time'})

  70. mw=soup.find_all('span', {'class': 'stonefont'})

  71. for i in range(len(mt)):

  72. moviename=mn[i].get_text()

  73. film_ting = ting[i].get_text()

  74. movietime=mt[i].get_text()

  75. moviewish=mw[i].get_text().encode('utf-8')

  76. for i in range(len(utf8List)):

  77. moviewish = moviewish.replace(utf8List[i], numList[i])

  78. print moviename,film_ting,movietime,moviewish

  79. spider = MaoyanSpider()

  80. spider.getNote()

解析访问,获取数据(最后一列是加密破解后的数据)。

Python爬虫杂记 - 字体文件反爬(二)

https://www.jianshu.com/p/0e2e1aa6d270

在线字体转化工具

https://www.fontke.com/tool/convfont/

猜你喜欢

转载自blog.csdn.net/baidu_32542573/article/details/82259865