2017-09-22 69 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) 

请确保您发送数据和报头是正确的。