巨量星图字体解密

由于星图加密文件一直再变化,所以需要重试多次,直到解密正确才返回

import os
import re
import requests
from fontTools.ttLib import TTFont


star_url = r'https://www.xingtu.cn/ad/creator/market?platform_source=1'
view_counts_1 = ''

# 解密映射字典
dict_list = {
    "uni30": "0",
    "uni31": "1",
    "uni32": "2",
    "uni33": "3",
    "uni34": "4",
    "uni35": "5",
    "uni36": "6",
    "uni37": "7",
    "uni38": "8",
    "uni39": "9",
    "uni2e": ".",
    "uni2c": ",",
    "uni77": "w",
    "uni25": "%",
    "uni2d": "-"
}


"""
:description: 通过requests库获取css文件->css文件并完成正则匹配,获取css地址
:params: star_url: 星图URL
:return: j: css文件地址
"""


def get_css_file(star_url):
    resp = requests.get(star_url)
    a = re.compile(r'rel="stylesheet" href="(?P<css_url>.*?)"', re.S)
    result = a.finditer(resp.text)
    for i in result:
        j = i.group('css_url')
        return j


"""
:description: 通过css地址获取字体文件地址->woff2,并返回woff2文件content内容
:return: resp.content: woff2文件content
"""


def get_font_url(int_num):
    # 读取css文件内容并返回
    css_content = requests.get(url=get_css_file(star_url))
    # 通过正则匹配出woff2文件地址
    css_a = re.compile(f'font-family:OPPOSans-Cloak;font-weight:{int_num};src:url\((?P<font_url>.*?\.woff2)', re.S)
    # print(css_a)
    url_result = css_a.finditer(css_content.text)
    # print(url_result)
    # 获取
    for k in url_result:
        # print(11111)
        font_url = k.group('font_url')
        # print(font_url)
        # 获取响应对象
        resp = requests.get(url=font_url)
        if resp.status_code != 200:
            print("woff2文件下载异常,请检查正则是否发生变化...")
        return resp.content


"""
:description: 返回xml文件中加密对应关系
:return: dict(merge_dict): xml文件映射关系.type:dict
"""


def download_font(int_):
    save_woff = get_desktop() + r'\巨量星图.woff2'
    with open(save_woff, 'wb') as f:
        f.write(get_font_url(int_))
    font = TTFont(save_woff)
    save_xml = get_desktop() + r'\巨量星图.xml'
    font.saveXML(save_xml)  # 转换为xml文件

    font = TTFont(save_woff)
    unicode_glyph = font.getBestCmap()
    dict_key_list = list(unicode_glyph.keys())
    dict_value_list = list(unicode_glyph.values())
    list_key_16 = []
    list_value = []
    for i in dict_key_list:
        list_key_16.append(hex(i))  # key转为16进制
    for j in dict_value_list:
        list_value.append(j)
    merge_dict = dict(zip(list_key_16, list_value))
    print(dict(merge_dict))
    return dict(merge_dict)


"""
:description: 获取Desktop绝对路径
:return: temp_file_path: Desktop文件夹绝对路径. format: C:\\Users\\用户名\\AppData\\Local\\Temp
"""


def get_desktop():
    # 获取桌面文件夹Desktop绝对路径
    fir_file_path = os.path.join(os.path.expanduser("~"), 'Desktop')
    temp_file_path = fir_file_path.replace(r"\Desktop", '\AppData\Local\Temp')
    if not os.path.exists(temp_file_path):
        os.makedirs(temp_file_path)
    return temp_file_path


"""
:description: 主函数
"""

def main(int_):
    view_counts_2 = view_counts_1.encode('raw_unicode_escape')
    str_view_counts = str(view_counts_2)
    str_view_counts = str_view_counts.strip("'")
    view_counts_list = str_view_counts.split(r"\\ue")
    num_list = []
    merge_dict_3 = download_font(int_)
    for i in range(1, len(view_counts_list)):
        temp_str = view_counts_list[i][:3]
        str_num = "0xe" + temp_str
        num_list.append(str_num)
    print(num_list)
    dict_zip_list = []

    for j in num_list:
        # try:
        #     dict_zip = merge_dict[j]
        # except:
        dict_zip = merge_dict_3[j]
        dict_zip_list.append(dict_zip)
    num_list_1 = []
    for num in dict_zip_list:
        xml_data = dict_list[num]
        num_list_1.append(xml_data)
    view_count = ''.join(num_list_1)

    print('uu', view_count)
    return view_count


if __name__ == "__main__":
    from time import sleep
    for i in range(10):
        try:
            main(700)
            break
        except:
            sleep(5)
            try:
                main(900)
                break
            except:
                continue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于使用Selenium来爬取星图,您可以按照以下步骤进行操作: 1. 安装Selenium:使用pip安装Selenium库,可以在命令行中执行以下命令: ``` pip install selenium ``` 2. 下载浏览器驱动:Selenium需要与特定的浏览器驱动程序配合使用,您可以根据自己使用的浏览器下载对应的驱动程序。常用的浏览器驱动有ChromeDriver和GeckoDriver(用于Firefox),您可以选择适合您的浏览器的驱动版本。 3. 配置驱动路径:将下载好的驱动程序所在路径添加到系统环境变中,或者在代码中指定驱动程序的路径。例如,如果您使用的是Chrome浏览器和ChromeDriver,可以在代码中设置如下: ```python from selenium import webdriver driver = webdriver.Chrome('/path/to/chromedriver') ``` 4. 使用Selenium进行页面操作:根据星图的网站结构和需求,通过Selenium模拟浏览器的操作,获取页面数据。一般来说,您可以使用`find_element_by_XXX`系列方法来定位网页元素,并使用`get_attribute`或`text`等方法获取元素的属性或文本内容。 5. 处理页面数据:根据您的需求,将获取到的页面数据进行处理和保存。您可以将数据保存到文件,或者存入数据库等。 请注意,使用Selenium进行爬取时,需要遵守网站的爬虫规则,并尊重网站的隐私政策。同时,为了避免给网站服务器带来过大的负担,建议设置合理的访问频率和延时。 希望以上信息对您有所帮助!如果您有任何其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值