您现在的位置是: 网站首页> 学习笔记> 爬虫 爬虫

字体反爬1

2021-04-11 [字体反爬] 3706人已围观

import requests
import re
import io
import base64
from lxml.html import etree
from fontTools.ttLib import TTFont
from concurrent.futures import ThreadPoolExecutor


url = "http://www.glidedsky.com/level/web/crawler-font-puzzle-2?page={}"

headers = {
}
total_values=[]
def get_code_list(response):
    '''
    处理字体文件,获取字体文件中数字与编码对应关系
    :param response: 请求响应文本
    :return: 字体文件中数字与编码对应关系字典,字典key为编码,val为数字
    '''
    # 从网页中提取base64编码的字体数据
    font_face = re.search('src: url\(data:font;charset=utf-8;base64,(.*)\) format\("truetype"\);', response.text).group(
        1)
    # 转化为可操作的字体文件
    ttf = TTFont(io.BytesIO(base64.b64decode(font_face)))

    # 获取字体文件中数字与编码对应关系
    code_list = ttf.getGlyphOrder()[1:]
    return code_list


def parse(page):
    print('page: {}'.format(page))
    response = requests.request("GET", url.format(page), headers=headers)
    if response.status_code != 200:
        with open('error_url.txt', 'a') as f:
            f.write(response.url + '\n')
        print(response.text)

    # 虚值与真实值对应关系
    code_list = get_code_list(response)
    response = etree.HTML(response.text)
    try:
        values = []
        for item in response.xpath('//div[@class="card-body"]//div[@class="col-md-1"]/text()'):
            if not item.strip():
                print('item--> ', item.strip)
            v = list(map(lambda x: 'uni'+ str(x).upper(), item.strip().encode('unicode-escape').decode().split('\\u')[1:]))

            ret = int(''.join([str(code_list.index(_)) for _ in v]))
            values.append(ret)
        return values
    except Exception as e:
        print(e)
        return parse(page)


with ThreadPoolExecutor(max_workers=10) as thread_pool:
    page_lsit = range(1, 1001)
    ret = thread_pool.map(parse, page_lsit)

    sum_value = 0
    for page_lsit in ret:
        print(page_lsit)
        sum_value += sum(page_lsit)
    print(sum_value)
# sum_val = 0
# for page in range(1, 1001):
#     v = parse(page)
#     sum_val += sum(v)
#     print(v)
#
# print(sum(sum_val))

上一篇:base64字体存为本地文件

下一篇:css反爬

相关文章

文章评论

暂无评论

添加评论





本栏推荐

站点信息

  • 建站时间:2021-01-01
  • 网站程序:Django 3.1.2
  • 文章统计:53篇
  • 文章评论:31条
  • 统计数据