1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| import base64
import requests from lxml import etree from fontTools.ttLib import TTFont from data import data as fontMap from redis import Redis from json import loads
parser = etree.XMLParser()
client = Redis()
headers = { "accept": "application/json, text/javascript, */*; q=0.01", "accept-language": "zh-CN,zh;q=0.9,en;q=0.8", "content-type": "application/x-www-form-urlencoded; charset=UTF-8", "origin": "https://www.python-spider.com", "referer": "https://www.python-spider.com/challenge/13", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36", "x-requested-with": "XMLHttpRequest" } url = "https://www.python-spider.com/api/challenge13"
def realNumer(value, tem): result = '' rev_maps = {v: k for k, v in tem.items()}
temp = value.split() num1 = temp[0] num2 = temp[1]
if len(temp) == 2: result = int(rev_maps[num1]) * 10 + int(rev_maps[num2]) * 1
if len(temp) == 3: num3 = temp[2] result = int(rev_maps[num1]) * 100 + int(rev_maps[num2]) * 10 + int(rev_maps[num3])
if len(temp) == 4: num3 = temp[2] num4 = temp[3] result = int(rev_maps[num1]) * 1000 + int(rev_maps[num2]) * 100 + int(rev_maps[num3]) * 10 + int(rev_maps[num4])
return result
def main(): sumList = []
for p in range(1, 101):
print(p) data = { "page": p } response = requests.post(url, headers=headers, data=data) res_data = loads(response.text) with open('font.ttf', 'wb') as f: f.write(base64.b64decode(res_data['woff']))
font = TTFont('font.ttf') font.saveXML('font.xml')
xml_data = open('font.xml', 'rb').read() tree = etree.fromstring(xml_data, parser=parser) reversed_dict = {v: k for k, v in fontMap.items()} d = dict() for k, v in reversed_dict.items(): for w in tree.xpath('//TTGlyph'): if k.strip() in str(etree.tostring(w, pretty_print=True), 'utf-8'): d[v] = w.xpath('./@name')[0].replace('uni', '&#x') print(d) for j in res_data['data']: if j['value']: real = realNumer(j['value'], d) sumList.append(real)
print(sum(sumList))
if __name__ == '__main__': main()
|