字庫反爬

58同城個人房源.png

檢查頁面元素,WHAT!!!,什么時間把價格這些關(guān)鍵字段變成這些鬼字符了。
查閱了相關(guān)資料,發(fā)現(xiàn)這是CSS3的一個新特性,允許在頁面中使用自己定義的字庫顯示。那好,我就把你的字庫存儲下來。

字庫.png

上圖標紅的部分其實就是字體庫的base64編碼后的結(jié)果,那我們將其解碼后通過python腳本將其保存成ttf文件,然后通過工具打開,我們發(fā)現(xiàn)了新大陸!
image.png

原來 對應的是8,其他的依次類推。

通過幾次測試發(fā)現(xiàn),鬼字符都是一樣的,只不過每次對應的數(shù)字順序不同,這時我們可以想到獲取字庫中的所有鬼字符,然后把這些字符使用PIL繪制出來,然后使用pytesseract識別一下不就拿到對應關(guān)系了嗎?剩下的就是正則替換??

image.png

完整代碼github地址

# coding=utf-8

import re

import requests
from PIL import ImageDraw, ImageFont, Image
from fontTools.ttLib import TTFont
import pytesseract
import base64
from scrapy.selector import Selector


def get_html():
    url = 'https://bj.58.com/hezu/35762485615942x.shtml?from=1-list-2&iuType=z_0&PGTID=0d3090a7-0000-1d18-cd0c-225cc1843040&ClickID=2&adtype=3'
    r = requests.get(url)
    html = r.content
    with open('test.html','w') as f:
        f.write(html)
    return html

def get_origin_price(html):
    '''
    獲取html中的租金信息
    '''
    sel = Selector(text=html)
    price = sel.xpath('//span[@class="c_ff552e"]/b/text()').extract_first()
    return price

class Font(object):
    def __init__(self,html):
        self.html = html
        self.regex = re.compile(r'base64,(.*)\'\) format')
    
    def save_ttf(self,ttf_path='test.ttf'):
        '''
        存儲字庫
        '''
        font_content = base64.b64decode(self.regex.search(html).group(1))
        with open(ttf_path,'wb') as f:
            f.write(font_content)
        return font_content

    def save_ttf_xml(self, ttf_path='test.ttf', xml_path='test.xml'):
        '''
        存儲字體庫的xml
        '''
        font = TTFont(ttf_path)
        font.saveXML(xml_path)

    def get_all_char(self, ttf_path='test.ttf'):
        '''
        獲取字庫中的所有字符
        '''
        font = TTFont(ttf_path)
        uni_char_list = []
        for table in font['cmap'].tables:
            for char_code, glyph_name in table.cmap.items():
                uni_char_list.append(unichr(char_code))
        uni_char_list = list(set(uni_char_list))
        return ''.join(uni_char_list)

    def get_mapping(self, content, ttf_path='test.ttf'):
        '''
        獲取字庫映射
        '''
        font = ImageFont.truetype(ttf_path, size=21, encoding="unic")
        width = 150
        height = 20
        image = Image.new('RGB', (width, height), (255, 255, 255))
        draw = ImageDraw.Draw(image)
        draw.text((10, 10), content, font=font, fill="#000000")
        image.save('test.jpg', dpi=(300.0, 300.0))
        mapping_value = pytesseract.image_to_string(image, config='-psm 3')
        result = dict(zip(content, mapping_value))
        return result

if __name__ == '__main__':
    html = get_html()
    price = get_origin_price(html)
    print u'原始租金:{}'.format(price)
    font_instance = Font(html)
    font_instance.save_ttf()
    font_instance.save_ttf_xml()
    process_string = font_instance.get_all_char()
    print u'奇葩字符是:{}'.format(process_string)
    mapping_value = font_instance.get_mapping(process_string)
    print mapping_value
    for each_str in price:
        price = re.sub(each_str, mapping_value[each_str], price)
    print u'處理后租金:{}'.format(price)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容