2016-11-08 13 views
0

scrapy pythonを使用してjavascriptコンテンツからデータを取得するには?このようなjavascriptの外観javascriptからscthon pythonを使用してPythonにデータを取得

<script type="text/javascript"> 
    var ad_reply_url = "http://www2.mudah.my/ar/send/0?ca=3_s&id=49825097&l=0"; 
    var mcvl = ""; 
    var images = [ 
    'http://img.rnudah.com/images/13/133608119523265.jpg', 
    'http://img.rnudah.com/images/13/135608116569903.jpg', 
    'http://img.rnudah.com/images/13/137608113616541.jpg', 
    'http://img.rnudah.com/images/13/139608119186498.jpg' 
    ]; 
var thumbnails = [ 
    'http://img.rnudah.com/thumbs/13/133608119523265.jpg', 
    'http://img.rnudah.com/thumbs/13/135608116569903.jpg', 
    'http://img.rnudah.com/thumbs/13/137608113616541.jpg', 
    'http://img.rnudah.com/thumbs/13/139608119186498.jpg' 
];</script> 

だから、私がしたいものです。私はvarの画像からデータを得て、このようなデータを印刷します

['http://img.rnudah.com/images/13/133608119523265.jpg','http://img.rnudah.com/images/13/135608116569903.jpg', 'http://img.rnudah.com/images/13/137608113616541.jpg','http://img.rnudah.com/images/13/139608119186498.jpg' ]; 

誰でも手伝ってもらえますか?ありがとう。

答えて

0

私はScrapy Pythonを使用していません。普通のPythonだけです。 これは、しかし非常に簡単です:

コードサンプル:

import ast 
import re 

page_source = ''' 
<script type="text/javascript"> 
    var ad_reply_url = "http://www2.mudah.my/ar/send/0?ca=3_s&id=49825097&l=0"; 
    var mcvl = ""; 
    var images = [ 
    'http://img.rnudah.com/images/13/133608119523265.jpg', 
    'http://img.rnudah.com/images/13/135608116569903.jpg', 
    'http://img.rnudah.com/images/13/137608113616541.jpg', 
    'http://img.rnudah.com/images/13/139608119186498.jpg' 
    ]; 
var thumbnails = [ 
    'http://img.rnudah.com/thumbs/13/133608119523265.jpg', 
    'http://img.rnudah.com/thumbs/13/135608116569903.jpg', 
    'http://img.rnudah.com/thumbs/13/137608113616541.jpg', 
    'http://img.rnudah.com/thumbs/13/139608119186498.jpg' 
];</script> 
''' 

variables = re.findall('(?si)var(.*?);', page_source) 

var_collection = {} 
for var in variables: 
    var = var.strip() 
    var_key = var.split(' = ')[0] 
    var_value = ast.literal_eval(var.split(' = ')[1]) 
    var_collection.update({var_key: var_value}) 

print(var_collection['images']) 

出力:

['http://img.rnudah.com/images/13/133608119523265.jpg', 'http://img.rnudah.com/images/13/135608116569903.jpg', 'http://img.rnudah.com/images/13/137608113616541.jpg', 'http://img.rnudah.com/images/13/139608119186498.jpg'] 

関連: https://stackoverflow.com/a/18108644/295246

+0

は大丈夫..おかげで私にヒントを与える...ちょうど今Iあなたのコードを操作しようとしていた今、私は欲しいものを得ました...ありがとう! :) – shahril

+0

@shahril喜んで助けた。あなたの裁量により、この解答をあなたの解決策としてupvoteまたは受け入れてください。ありがとう! –

関連する問題