<script language="JavaScript" type="text/javascript+gk-onload"> SKART = (SKART) ? SKART : {};
SKART.analytics = SKART.analytics || {};
SKART.analytics["category"] = "television";
SKART.analytics["vertical"] = "television";
SKART.analytics["supercategory"] = "homeentertainmentlarge";
SKART.analytics["subcategory"] = "television"; </script>
You can use the Selector
's built-in support for regular expressions through re()
:
pattern = r'SKART\.analytics\["category"\] = "(\w+)";'
response.xpath('//script[@type="text/javascript+gk-onload"]').re(pattern)
Demo (using scrapy shell
):
$ scrapy shell index.html
In []: pattern = r'SKART\.analytics\["category"\] = "(\w+)";' In []: response.xpath('//script[@type="text/javascript+gk-onload"]').re(pattern)
Out[]: [u'television']
https://stackoverflow.com/questions/29163395/scrapy-and-xpath-to-extract-data-from-javascript-code