就律 2022-10-18 13:44 采纳率: 100%
浏览 18
已结题

js解析为xml遇const报错

问题遇到的现象和发生背景

将string解析成xml报错

用代码块功能插入代码,请勿粘贴截图
def parse(self, response):
    # 解析json,获取某个变量var的值
    # /html/body/script[1]/text()
    print(1)
    json_str = response.xpath('//html/body/script[1]/text()').extract_first()

    demo = response.text
    soup = BeautifulSoup(demo, 'lxml')
    script = soup.select('body script')[0].string
    print(script)
    script_text = js2xml.parse(script, "text/xml")
    script_tree = js2xml.pretty_print(script_text)
    selector = etree.HTML(script_tree)
运行结果及报错内容

img

calmjs.parse.exceptions.ECMASyntaxError: Unexpected 'const' at 9:3 after ';' at 6:48

我想要达到的结果

正确解析

  • 写回答

2条回答 默认 最新

  • 就律 2022-10-27 14:58
    关注

    发现可能是版本问题,js2xml解析不了const关键字,然后我就直接粗暴的替换const为var ,然后正常解析。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 已采纳回答 10月27日
  • 创建了问题 10月18日