首页 > 技术文章 > 爬虫(一)-东方财富网字体

shenyiyangle 2019-04-13 20:14 原文

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

字体方块

 

后缀.woff是字体

数字是乱码,需要把乱码改成数字

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)

r.encoding=r.apparent_encoding

print(r.text)

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']

数据结果:

  

 

字体

  cmap表里记录有unicode索引和文字关系https://www.cnblogs.com/shenyiyangle/p/10700156.html

  自定义字体的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)

 

结果:

 

推荐阅读