2017-09-22 115 views
1

我正在使用scrapy +飛濺插件,我有一個按鈕,通過ajax觸發下載事件,我需要獲取下載的文件,但不知道如何。從js點擊事件的Scrapy飛濺下載文件

我的LUA腳本是一樣的東西從我的蜘蛛這個

function main(splash) 
     splash:init_cookies(splash.args.cookies) 
     assert(splash:go{ 
      splash.args.url, 
      headers=splash.args.headers, 
      http_method=splash.args.http_method, 
      body=splash.args.body, 
     }) 
     assert(splash:wait(0.5)) 
        local get_dimensions = splash:jsfunc([[ 
      function() { 
       var rect = document.querySelector('a[aria-label="Download XML"]').getClientRects()[0]; 
       return {"x": rect.left, "y": rect.top} 
      } 
     ]]) 
     splash:set_viewport_full() 
     splash:wait(0.1) 
     local dimensions = get_dimensions() 
     -- FIXME: button must be inside a viewport 
     splash:mouse_click(dimensions.x, dimensions.y) 
     splash:wait(0.1) 
     return splash:html() 
    end 

我的請求對象:

yield SplashFormRequest(self.urls['url'], 
          formdata=FormBuilder.build_form(response, some_object[0]), 
          callback=self.parse_cuenta, 
          cache_args=['lua_source'], 
          endpoint='execute', 
          args={'lua_source': self.script_click_xml}) 

在此先感謝

+0

嗨@delpo,你有解決方案嗎? –

+0

嘿@SanoopPK,我還沒有解決 – delpo

回答

1

我只是SplashFormRequest嘗試這樣做,它看起來像飛濺將不適合你。相反,您可以使用python Requests發送相同的Ajax請求。

這裏有一個例子

data = {'__EVENTTARGET': 'main_0$body_0$lnkDownloadBio', 
     '__EVENTARGUMENT': '', 
     '__VIEWSTATE': viewstate, 
     '__VIEWSTATEGENERATOR': viewstategen, 
     '__EVENTVALIDATION': eventvalid, 
     'search': '', 
     'filters': '', 
     'score': ''} 

HEADERS = { 
     'Content-Type':'application/x-www-form-urlencoded', 
     'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36', 
     'Accept': 'text/html, application/xhtml + xml, application/xml;q = 0.9, image/webp, image/apng, */*;q = 0.8' 
    } 

data = urllib.urlencode(data) 
r = requests.post(submit_url, data=data, allow_redirects=False, headers=HEADERS) 
filename = 'name-%s.pdf' % item['first_name'] 
with open(filename, 'wb') as f: 
    f.write(r.content) 

請確保您發送數據和報頭是正確的。