Win7下的python: 通過amd64的二進制文件安裝, 位置在 C:\Users\Milton\AppData\Local\Programs\Python\Python37\
安裝pip3
C:\Users\Milton\AppData\Local\Programs\Python\Python37\Scripts\easy_install.exe pip
通過pip3安裝fonttools
C:\Users\Milton\AppData\Local\Programs\Python\Python37\Scripts\pip3.7.exe install fonttools
代碼例子, 貓眼的字體反爬蟲已經升級了, 不再是簡單的順序關系, 這樣取到的值依然是錯的. 需要根據根據各個字體里面的字體定義TTGlyph.contour的值去判斷具體數字.
import requests import re import os from fontTools.ttLib import TTFont class MaoYan(object): def __init__(self): self.url = 'https://maoyan.com/films/42964' 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" } # 發送請求獲得響應 def get_html(self, url): response = requests.get(url, headers=self.headers) return response.content # 創建 self.font 屬性 def create_font(self, font_file): # 列出已下載文件 file_list = os.listdir('./fonts') # 判斷是否已下載 if font_file not in file_list: # 未下載則下載新庫 print('不在字體庫中, 下載:', font_file) url = 'http://vfile.meituan.net/colorstone/' + font_file new_file = self.get_html(url) with open('./fonts/' + font_file, 'wb') as f: f.write(new_file) # 打開字體文件,創建 self.font屬性 self.font = TTFont('./fonts/' + font_file) self.font.saveXML('./fonts/' + font_file + '.xml') # 把獲取到的數據用字體對應起來,得到真實數據 def modify_data(self, data): print(data); # 獲取 GlyphOrder 節點 gly_list = self.font.getGlyphOrder() # 前兩個不是需要的值,截掉 gly_list = gly_list[2:] # 枚舉, number是下標,正好對應真實的數字,gly是亂碼 for number, gly in enumerate(gly_list): # 把 gly 改成網頁中的格式 gly = gly.replace('uni', '&#x').lower() + ';' # 如果 gly 在字符串中,用對應數字替換 if gly in data: data = data.replace(gly, str(number)) # 返回替換后的字符串 return data def start_crawl(self): html = self.get_html(self.url).decode('utf-8') # 正則匹配字體文件 font_file = re.findall(r'vfile\.meituan\.net\/colorstone\/(\w+\.woff)', html)[0] print(font_file); self.create_font(font_file) # 正則匹配星級 star = re.findall(r'<span class="index-left info-num ">\s+<span class="stonefont">(.*?)</span>\s+</span>', html)[0] star = self.modify_data(star) # 正則匹配評論的人數 people = ''.join(re.findall(r'''<span class='score-num'><span class="stonefont">(.*?萬)</span>(人評分)</span>''', html)[0]) people = self.modify_data(people) # 正則匹配累計票房 ticket_number = ''.join(re.findall(r'''<span class="stonefont">(.*?)</span><span class="unit">(億)</span>''', html)[0]) ticket_number = self.modify_data(ticket_number) print('用戶評分: %s 星' % star) print('評分人數: %s' % people) print('累計票房: %s' % ticket_number) if __name__ == '__main__': maoyan = MaoYan() maoyan.start_crawl()
.