Jolly23
V2EX  ›  问与答

拿到的数据是 dwr.engine 接口提供的,请问怎么用 python 分析,或者解析到里面的 xml?

  •  
  •   Jolly23 · Dec 1, 2016 · 2757 views
    This topic created in 3479 days ago, the information mentioned may be changed or developed.

    如题,从学校新闻接口抓到的数据,是在浏览器网络监听的 portalAjax.getNewsXml.dwr 这里看到的响应, 用 python request post 方法调用的返回文本是:

    //#DWR-INSERT //#DWR-REPLY dwr.engine._remoteHandleCallback('0','0',"\n<list><pagecount>3641</pagecount><item>\n\n<link></link>\n<description></description>\n<category></category>\n<pubdate>Thu, 17 Nov 2016 00:23:21 GMT</pubdate>\n<guid></guid>\n<dc:creator xmlns:dc="&lt;a href=" http:="" <a="" href="&lt;a href=" http:="" purl.org"="" rel="nofollow">http://purl.org" rel="nofollow"><a href="http://purl.org" rel="nofollow">purl.org</a>="" dc="" elements="" 1.1="" "="" rel="nofollow">http://purl.org/dc/elements/1.1/"></dc:creator>\n<dc:date xmlns:dc="&lt;a href=" http:="" <a="" href="&lt;a href=" http:="" purl.org"="" rel="nofollow">http://purl.org" rel="nofollow">purl.org="" dc="" elements="" 1.1="" "="" rel="nofollow">http://purl.org/dc/elements/1.1/">Thu, 17 Nov 2016 00:23:21 GMT</dc:date>\n<xwbh>147934233182128368</xwbh>\n<color>null</color>\n<spanpic>pic</spanpic>\n<lmmc></lmmc>\n<enclosure url="&lt;a href=" http:="" <a="" href="&lt;a href=" http:="" www.ynnu.edu.cn"="" rel="nofollow">http://www.ynnu.edu.cn" rel="nofollow">www.ynnu.edu.cn="" UserFiles="" Image="" 147934226721288544.png"="" rel="nofollow">http://www.ynnu.edu.cn/UserFiles/Image/147934226721288544.png" type="image/pjpeg"/>\n</item></list>");

    请问如何使用 py 截取里面的 xml ,我试着用字符串寻找到 xml 头部和尾部,然后调用 xml.etree 分析,但初始化 xml 时报错:

    xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 784

    如何解决?

    5 replies    2016-12-01 17:39:39 +08:00
    stamaimer
        1
    stamaimer  
       Dec 1, 2016 via iPhone
    你把 list 之前的内容删掉试试?
    Jolly23
        2
    Jolly23  
    OP
       Dec 1, 2016
    @stamaimer 对,是这么做了,这个 xml 不标准,标签有些问题
    stamaimer
        3
    stamaimer  
       Dec 1, 2016 via iPhone
    我猜 bs4 里面应该有可以修正不规范的文档的功能。
    broono
        4
    broono  
       Dec 1, 2016
    不打算用正则先匹配出完整的树出来吗
    slysly759
        5
    slysly759  
       Dec 1, 2016 via Android
    哈哈原来有遇到过, stack 上有解决方案,个人博客上有提到过| ू•ૅω•́)ᵎᵎᵎ
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4436 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 10:05 · PVG 18:05 · LAX 03:05 · JFK 06:05
    ♥ Do have faith in what you're doing.