我关注了
html_source = """{"linkparam":"CDAQ46598omxw=","linkmetadata":{"weblinkmetadata":{"url":"/service_ajax","sendPost":true}},"formfield":{"action":"CAUaMVVnd2t2Z1htRGl3OXAtS0FVaUY0QWFBQkNRLjhtZmduZEgzWXI4OG1maDFJMjRiV0gwATgAShUxMDIwMTQzMTg0NzMxMTE4NzMxNzBaGFVDQjBkMEpMbjFXY0dZY3d3Wjg3ZDJMQXAA","clientActions":[{"formaction":{"voteCount":{"accessibility":{"accessibilityData":{"label":"11 status"}},"simpleText":"11"},"formstatus":"FORM"}}]}}
#below part i want to extract from page including curly braces
{"linkparam":"CDAQ46597omxw=","linkmetadata":{"weblinkmetadata":{"url":"/service_ajax","sendPost":true}},"formfield":{"action":"CAUaMVVnd2t2Z1htRGl3OXAtS0FVaUY0QWFBQkNRLjhtZmduZEgzWXI4OG1maDFJMjRiV0gwATgAShUxMDIwMTQzMTg0NzMxMTE4NzMxNzBaGFVDQjBkMEpMbjFXY0dZY3d3Wjg3ZDJMQXAA","clientActions":[{"formaction":{"voteCount":{"accessibility":{"accessibilityData":{"label":"11 status"}},"simpleText":"11"},"formstatus":"FORM"}}]}}
#above part i want to extract from page including curly braces
{"linkparam":"CDAQ46448omxw=","linkmetadata":{"weblinkmetadata":{"url":"/service_ajax","sendPost":true}},"formfield":{"action":"BQkNRLjhtZmduZEgzWXI4OG1maDFJMjRiV0gwATgAShUxMDIwMTQzMTg0NzMxMTE4NzMxNzBaGFVDQjBkMEpMbjFXY0dZY3d3Wjg3ZDJMQXAA","clientActions":[{"formaction":{"voteCount":{"accessibility":{"accessibilityData":{"label":"11 status"}},"simpleText":"11"},"formstatus":"FORM"}}]}}"""a
m = re.search(r"\{(.*?)\}", html_source)
我想从页面字符串中提取这部分
{"linkparam":"CDAQ46597omxw=","linkmetadata":{"weblinkmetadata":{"url":"/service_ajax","sendPost":true}},"formfield":{"action":"CAUaMVVnd2t2Z1htRGl3OXAtS0FVaUY0QWFBQkNRLjhtZmduZEgzWXI4OG1maDFJMjRiV0gwATgAShUxMDIwMTQzMTg0NzMxMTE4NzMxNzBaGFVDQjBkMEpMbjFXY0dZY3d3Wjg3ZDJMQXAA","clientActions":[{"formaction":{"voteCount":{"accessibility":{"accessibilityData":{"label":"11 status"}},"simpleText":"11"},"formstatus":"FORM"}}]}}
答案 0 :(得分:0)
您的数据看起来像是json个项目的列表,这些项目由注释定界(以“#”开头的行)。
因此,您可以用“,”代替注释,并用“ [”和“]”包装数据以创建json列表。
import re
html_source = re.sub(r'#.*?\n', ',', html_source, flags=re.DOTALL)
html_source = '['+html_source+']'
然后,您可以使用json库来解析此项目列表并提取第二个项目:
import json
import pprint
data = json.loads(html_source)
pprint.pprint(data[2])
您得到:
{'formfield': {'action': 'BQkNRLjhtZmduZEgzWXI4OG1maDFJMjRiV0gwATgAShUxMDIwMTQzMTg0NzMxMTE4NzMxNzBaGFVDQjBkMEpMbjFXY0dZY3d3Wjg3ZDJMQXAA',
'clientActions': [{'formaction': {'formstatus': 'FORM',
'voteCount': {'accessibility': {'accessibilityData': {'label': '11 '
'status'}},
'simpleText': '11'}}}]},
'linkmetadata': {'weblinkmetadata': {'sendPost': True,
'url': '/service_ajax'}},
'linkparam': 'CDAQ46448omxw='}
如果您没有评论...
您可以这样做:
# insert ',' as a delimiter
html_source = html_source.replace('{"linkparam"', ', {"linkparame"')
# drop first comma
html_source = html_source[2:]
html_source = '[' + html_source + ']'