2017-12-18 104 views
2

我在调查subj,因为有一个网站在点击一个链接后可以访问部分数据。点击JS链接飞溅

我有一个这样的脚本:

#!/usr/bin/env python 

script=""" 
splash:go(splash.args.url) 
splash:wait(10) 

splash:runjs("$('a[data-bet-type=FixedPlace]')[0].click()") 
splash:wait(10) 

return { 
    -- html = splash:html(), 
    png = splash:png(), 
    -- har = splash:har(), 
} 
""" 

from requests import post 
from json import dumps, dump 
from base64 import b64decode 
from contextlib import suppress 

if 1: 
    endpoint='http://localhost:8050/run' 
    j={ 
     'lua_source': script, 
     'url': 'https://www.odds.com.au/horse-racing/bunbury/race-1', 
     'timeout': 90, 
     'headers': { 
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0' 
     } 
    } 

r=post(endpoint, headers={'Content-Type': 'application/json'}, data=dumps(j)) 
print(r.status_code) 
if r.status_code != 200: 
    print(r.text) 
    exit() 
j=r.json() 

for _ in ('html', 'har'):   
    with suppress(KeyError), open('1.'+_, 'w') as f: f.write(j[_]) 
with suppress(KeyError), open('1.png', 'wb') as f: f.write(b64decode(j['png'])) 

if 'har' in j: 
    for entry in j['har']['log']['entries']: 
     url=entry['request']['url'] 
     if url.startswith('https://www.odds.com.au'): print(url) 

但是,尽管这是完全渲染页面,点击不会发生。我已经尝试与ya.ru和相同的方法工作(但在这种情况下,它是一个按钮)。我没有想法。尝试设置UA,使用js_source等待(这不是很快,那个页面),但不能点击一个东西,虽然我看到该元素被发现。

+1

你使用的是什么版本的闪屏?在Splash 2.1中有'splash:mouse_click'([示例](http://splash.readthedocs.io/en/latest/scripting-ref.html#splash-mouse-click))。也可能值得一读([触发jquery点击](https://stackoverflow.com/questions/1694595/can-i-call-jquery-click-to-follow-an-a-link-if-i-havent-绑定事件手)) – Adelin

+0

@Adelin我已经从主打3.0,并且mouse_click工作。 – Pooh

回答

1

,你与你的CSS选择器定位的a元素是不能点击 - 看到它href值是如何设置链接到什么:

<a href="javascript:void(-1);" data-event-id="665605" data-bet-type="FixedPlace">place</a> 

您需要点击父li元素代替:

$('.bettype__switcher li:last-child')[0].click() 

为我工作 - 获得选定的“地点”投注类型。