2017-12-18 98 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() 

爲我工作 - 獲得選定的「地點」投注類型。