東方財富網練習,字體

連接:http://data.eastmoney.com/bbsj/201903/yjbb.html

數據內容是方塊,找到字體文件
html

 

瀏覽器抓包後綴.woff是字體json

post數據,數字是亂碼,須要把亂碼改爲數字api

url="http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js"瀏覽器

headers={
"Referer": "http://data.eastmoney.com/bbsj/201903/yjbb.html",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}
data={
"type": "YJBB21_YJBB",
"token": "70f12f2f4f091e459a279469fe49eca5",
"st": "latestnoticedate",
"sr": "-1",
"p": "1",
"ps": "50",
"js": "var rIKyuApR={pages:(tp),data: (x),font:(font)}",
"filter": "(securitytypecode in ('058001001','058001002'))(reportdate=^2018-09-30^)",
"rt": "51838114"
}
r=requests.post(url=url,headers=headers,data=data)
r.encoding=r.apparent_encoding
print(r.text)app

r.encoding=r.apparent_encodingpost

print(r.text)字體

#用jupyter打開比較方便url

org_data=r.text[r.text.index("{"):] json_str = or_data.replace('data:', '"data":').replace('pages:', '"pages":').replace('font:', '"font":') json_data = json.loads(json_str)

#字體文件

 font_url = json_data['font']['WoffUrl']spa

數據結果:3d

  

 

字體

  cmap表裏記錄有unicode索引和文字的關係,參考http://www.javashuo.com/article/p-nfmrkysh-by.html

亂碼緣由

  自定義字體的unicode索引和字形的關係與標準關係不同

解決方法

  創建聯繫,讓自定義字體的unicode索引和文字對應,全局替換

 

找關係

TTFont解析字體

font = requests.get(font_url, headers=header, timeout=30) font_name = font_url.split("/")[-1] with codecs.open(font_name, 'wb') as f:   f.write(font.content) font_map = TTFont(font_name).getBestCmap()#用getBestCmap()能夠獲得字體index和glphy的映射關係
「」「{120: 'x',
 57960: 'bgldyy',
 57971: 'qqdwzl',
 58817: 'whyhyx',
 59299: 'wqqdzs',
 60397: 'zbxtdyc',
 60633: 'zwdxtdy',
 60650: 'zrwqqdl',
 61125: 'bdzypyc',
 62069: 'sxyzdxn',
 62669: 'nhpdjl'}」「」

font_map裏是unicode和字形的關係,key是unicode的十進制數字,value表示字形標記,index轉換成unicode:

font_index = [hex(key).upper().replace('0X', '&#x') +';' for key in font_map.keys()]
「」「['x',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '']」「」

unicode索引和文字的關係,能夠本身手動找,東方財富網頁面上有。

 font_mapping = json_data['font']['FontMapping']
 replace_dict= {i['code']: str(i['value']) for i in font_map}
」「」
{'': '7',
 '': '1',
 '': '9',
 '': '3',
 '': '4',
 '': '8',
 '': '2',
 '': '0',
 '': '6',
 '': '5'}
」「」

#全局替換

for k, v in replace_dict.items():   
  json_str = json_str.replace(k, v)
finall_data = json.loads(json_str)

 

結果:

保存一下。

 

相關文章
相關標籤/搜索