Skip to content Skip to sidebar Skip to footer

Converting 'bs4.element.navigablestring' To Json

I need to convert a bs4.element.NavigableString (which is from beautiful soup: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring) into a json string. I am usin

Solution 1:

Well, that string isn't valid JSON. Part of it is valid JSON.

In particular, it's a line of JavaScript code that assigns a JavaScript literal that happens to be written as valid JSON to a my_object variable.

If you want to parse the JSON, you have to separate it out from the rest of the statement. For example:

s = '''my_object = {"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}'''
jsvar, _, jsvalue = s.partition('=')
value = json.loads(jsvalue)

Post a Comment for "Converting 'bs4.element.navigablestring' To Json"