0

In scraping a website, I am getting given javascript code as a response from the server.

document.write("<script src='/src/one/data.cached.js?ver=9153'></script>");
amorphic.setApplication('one');
amorphic.setSchema(
  {
    "Address": {"customer": 1},
    "Person": {"customer": 1},
    "Phone": {"customer": 1}
  }
);

So, how can I extract this json data from the response using python?

  {
    "Address": {"customer": 1},
    "Person": {"customer": 1},
    "Phone": {"customer": 1}
  }
abhishake
  • 763
  • 9
  • 16
  • 1
    Possible duplicate of [Parsing values from a JSON file in Python](http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python) – GilZ Dec 08 '16 at 08:45
  • you can use regex to remove unwanted text and extract dictinoary or there must be option for to extract response data directly – Prashant Puri Dec 08 '16 at 08:47
  • did you try just to call dict on your object? like this `dict(your_object)`. Also, try `your_object['Address']` – ettanany Dec 08 '16 at 08:49
  • What you're showing looks like JSONP, not JSON. That's usually only done from client-side code, server code can request ordinary JSON. – Barmar Dec 08 '16 at 08:54
  • @ettanany , I am getting the json data wrapped inside a javascript object. I don't think that I can get the json data directly by using your_object['Address']. – abhishake Dec 08 '16 at 08:55
  • I can not understand! what's `helloWorld.LoremIpsum` in your case? Can you access `{"Address": ...}` in your javascript code? Are you trying to use Python in your frontend application or calling an API? – ettanany Dec 08 '16 at 08:57
  • No, now it's a Javascript program. – Barmar Dec 08 '16 at 09:40
  • You can use a regular expression to search for the call to `amorphic.setSchema`, and then extract the object literal in the argument. – Barmar Dec 08 '16 at 09:42
  • Thanks @Barmar ... can you provide a link to the tutorial? – abhishake Dec 08 '16 at 09:44
  • www.regular-expression.info is a great site with a regular expression tutorial – Barmar Dec 08 '16 at 09:44

1 Answers1

1

Finally extracted the required data from the response using Regular expression operations - re package.

Seems like this is the only way to extract json data from javascript response.

Heartily thanks to Prashant Puri and Barmar for their quick and great help.

Community
  • 1
  • 1
abhishake
  • 763
  • 9
  • 16