在内部使用ajaxjson解析html

2024-10-03 00:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的文件要用Python解析(从废弃开始):

some HTML and JS here...
SomeValue = 
{
     'calendar': [
     {       's0Date': new Date(2010, 9, 12),
             'values': [
                     { 's1Date': new Date(2010, 9, 17), 'price': 9900 },
                     { 's1Date': new Date(2010, 9, 18), 'price': 9900 },
                     { 's1Date': new Date(2010, 9, 19), 'price': 9900 },
                     { 's1Date': new Date(2010, 9, 20), 'price': 9900 },
                     { 's1Date': new Date(2010, 9, 21), 'price': 9900 },
                     { 's1Date': new Date(2010, 9, 22), 'price': 9900 },
                     { 's1Date': new Date(2010, 9, 23), 'price': 9900 }]
     },
     'data': [{
     index: 0,
     serviceClass: 'Economy',
     prices: [9900, 320.43, 253.27],
     eTicketing: true,
     segments: [{
             indexSegment: 0,
             stopsCount: 1,
             flights: [{
                     index: 0,

... and a lot of nested data and again HTML and JS...

我需要解析它并提取所有json数据。现在我使用regex和cleaning all'\n'和'\t'以及eval()函数将其转换为Python字典。。我真的不喜欢这个解决方案,尤其是eval()。但是我查看了beauthoulsoup和lxml,没有找到有助于解析它的东西。
你能为这个任务推荐比regex和eval()更好的方法吗?
页面示例:http://codepaste.ru/3830/


Tags: and文件newdatadateindexherehtml
1条回答
网友
1楼 · 发布于 2024-10-03 00:29:52

aarrghhh no regex dont use regex no regex no no nooooooo


使用json模块处理JSON数据:

import json
json.loads( <string> )

使用BeautifulSouplxml处理html页面的解析:

^{pr2}$

如果您需要特定的帮助,您需要提供特定的数据,例如包含此数据的标记的类。例如,您可以soup.findAll脚本标记,然后剥离一些行以获取JSON,然后将其输入json.loads。在

相关问题 更多 >