1
<script type="text/javascript">

define('roomsAndRatesData', function() {
var roomsAndRatePlans = {"propertyData":{"bookingFeeMessageEnabled":true}},{"rooms":{"24-285501":{"locale":"en"}}}

};
</script>

There where other variable also define in that script. i just need data that are inside "var roomsAndRatePlans" variable can any one suggest solution to get only that variable data.

i have try other solutions but always retrieve all data inside that script tag and converting that data throws error.

so please provide me solution to get only particular data of that script

Thanks

4
  • 1
    You cannot retrieve it by XPath, you have to extract it by Regex. Commented Aug 6, 2018 at 11:33
  • Can you help me to create that Regex, as i don't have much idea about how to use Regex @thirdDeveloper Commented Aug 6, 2018 at 11:51
  • For me this looks like a nested dictionary. So try to separate the stuff after the = and then access like an dictionary Commented Aug 6, 2018 at 12:02
  • You can try using js2xml: github.com/scrapinghub/js2xml Commented Aug 6, 2018 at 14:07

1 Answer 1

4

You can use the js2xml library:

>>> import js2xml
>>> from js2xml.utils.vars import get_vars
>>> snippet = '''
var product = {"price": 10.93, "stock": false, "name": "JBL"};
var analytics = {"id": 1234};
'''
>>> get_vars(js2xml.parse(snippet))
{
    'product': {'price': 10.93, 'stock': False, 'name': 'JBL'},
    'analytics': {'id': 1234}
}

Your JS snippet seems to be incomplete, so I used a dummy snippet here. In your case, you may first have to get the JS code via:

>>> snippet = response.css('script:contains("roomsAndRatesData")::text').get()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.