2017-09-16 17 views
0

function内でbranchIdを取得したいのですが、couldntです。あなたは関数からどうやって得るかについて考えていますか?Scrapy get function from function()

item["branchId"] = row.xpath('//div[@class="branchprofile"]//script/text()').extract()[0] 

HTMLコード:

<div id="branchprofile"> 
<script> 
(function(k,v){RMVH.ANALYTICS.DataLayer.pushKV(k,v);}('branch',{"branchId":5112345,"companyName":"KLM","brandName":"London KLM",,"pageType":"Standard"})); </script> 

答えて

0

あなたは、XPathやないextractreメソッドを使用する必要があります。抽出はテキストだけを与える

item['branchId'] = response.xpath("//div[@id='branchprofile']/script/text()").re('branchId":\s*(\d+)')[0] 
+0

ありがとう。あなたのサンプルは、私に正規表現を使用する方法のビューを与えました。私は複数の出力を持っており、異なる正規表現を書いています。 – ferdi

0

あなたは@idを使用する必要があります@classを使用している:

>[1]: sel.xpath("//div[@id='branchprofile']/script/text()")[0].extract() 
<[1]: '\n(function(k,v){RMVH.ANALYTICS.DataLayer.pushKV(k,v);}(\'branch\',{"branchId":5112345,"companyName":"KLM","brandName":"London KLM",,"pageType":"Standard"})); '