由于星图加密文件一直再变化,所以需要重试多次,直到解密正确才返回
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