The source code of one page 我正在编写一个新闻精灵,我想通过python从脚本中获取pubtime值。目前我可以得到脚本的内容,如下:
{
site:'sports',
site_cname:'体育',
site_url:'',
title:'球爹喊话詹皇:想拿更多冠军 那就和我儿子搭档 ',
id:'20170802002470',
pubtime:'2017-08-02 06:22',
type:'2',
article_url:'',
sosokeys:{key1:'NBA',key2:'湖人',key3:'球爹',key4:'詹姆斯'},
tags:['NBA','湖人','球爹','詹姆斯'],
catalog:'basket',
catalog_full:'sports-basket-nba',
sub_nav:'nba',
topic:{name:'',cname:'',ztcatalog:''},
subName:{name:'basket',url:'', cname:'篮球'},
isShowLastAD:'',
tpl:
{dev:'nba',ver:'1.0.0.0',time:'20150512',type:'1',stype:''}
}
我试着用json.loads()方法将字符串传输到json对象,但失败。它抛出错误:
^{pr2}$在抛出此错误之前,我已将所有“'”替换为“”。对于这个错误,我知道原因可能是所有的键都应该用双引号括起来,但是这里有太多的键,我认为手动地用双引号将每个键括起来并不是最佳选择。目前,我不知道如何处理pubtime的值。欢迎提出任何建议。提前谢谢你。在
这里有一种使用js2xml的方法:
首先,获取您感兴趣的JavaScript代码:
然后,将其发送给
^{pr2}$js2xml.parse()
以获得一个解析树:您可以检查使用
js2xml.pretty_print()
解析的js2xml:您需要的数据是
||
二进制运算的right
操作数。可以在解析树上使用XPath来获取它:js2xml.utils.objects.make
用于根据以下内容构建Python对象:正如@Granitosaurus所提到的,对于这样一个任务来说,这似乎有点“过分”,但是当JSON数据不是100%JSON时(例如使用单引号),它可能会很有用
有一些工具可以解析json变量之类的,主要是^{} ,这是由制造scrapy的人开发的。
然而,通常简单的regex就足够了:
当然,在您的例子中,您将使用
response.body_as_unicode()
而不是预定义的text
变量来搜索整个html正文。在相关问题 更多 >
编程相关推荐