weixin_42062762 2019-08-04 02:25 采纳率: 75%
浏览 470
已采纳

如何提取XML这里的东西?

python爬取58同城的数据,得到的结果房价和面积被加密,通过百度知道它的形成过程
和原来的破解过程,但是现在有了一点点小变动,我想如何提取这段name和code
的值,求教!!!!图片说明
我想把map name对应的值和其code的值 如何提取出来。
在FontTools.ttLib里面 好像没有提取map的函数吧?
之前代码:

import base64
from io import BytesIO
from fontTools.ttLib import TTFont
import requests
import re
from lxml import etree


for i in range(1,3):
    url = 'https://sz.58.com/zufang/pn'+str(i)
    res = requests.get(url)
    #print(res.text)
    bs64_str = re.findall("charset=utf-8;base64,(.*?)'\)", res.text)[0]
    bin_data = base64.decodebytes(bs64_str.encode())
    with open('test'+str(i)+'.otf','wb') as f:
        f.write(bin_data)
    font = TTFont(BytesIO(bin_data))
    font.saveXML('test'+str(i)+'.xml')
    uniList = font['glyf'].keys()
    c = font['cmap'].tables[0].ttFont.tables['cmap'].tables[0].cmap

    结果为{38006: 'glyph00005', 38287: 'glyph00007', 39228: 'glyph00004', 39499: 'glyph00003', 40506: 'glyph00010', 40611: 'glyph00006', 40804: 'glyph00009', 40850: 'glyph00001', 40868: 'glyph00002', 40869: 'glyph00008'}
{38006: 'glyph00009', 38287: 'glyph00003', 39228: 'glyph00005', 39499: 'glyph00001', 40506: 'glyph00006', 40611: 'glyph00002', 40804: 'glyph00010', 40850: 'glyph00008', 40868: 'glyph00004', 40869: 'glyph00007'}

所以我如何提取图片中map name和code所对应的值?
  • 写回答

1条回答 默认 最新

  • ivan_prajak 2019-08-05 09:54
    关注

    from lxml import etree

    parser = etree.HTMLParser(encoding="utf-8")
    xml = etree.parse('test1.xml',parser=parser) # etree加载本地XML文件,以第1个为例('test1.xml')
    name_list = xml.xpath('//cmap//map/@name') # 获取name值
    code_list = xml.xpath('//cmap//map/@code') # 获取code值
    code_list = [int(i, 16) for i in code_list] # code值16进制转10进制

    print(dict(zip(code_list, name_list)))

    望采纳!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog