VMLogin瀏覽器自動化 Local REST API 接口文檔說明

瀏覽器自動化允許您在VMLogin瀏覽器配置文件中自動執(zhí)行任務(wù)。從創(chuàng)建簡單的自動化腳本到復(fù)雜的Web爬蟲,可以搜索、收集Web數(shù)據(jù)并與之交互。

VMLogin Local API 中文版 說明文檔點擊查看

VMLogin Local API 英文版 說明文檔點擊查看


VMLogin瀏覽器自動化基于Selenium WebDriver,同時也支持Puppeteer等自動化框架:通常情況下,如果您運行Selenium代碼,首先將連接到Chrome驅(qū)動,然后設(shè)置您所需要的功能。而將VMLogin與Selenium代碼結(jié)合使用時,您無需這樣操作。您將使用Web Driver程序,通過本地端口連接到VMLogin應(yīng)用或某瀏覽器配置文件,設(shè)置所需功能,在預(yù)定義的瀏覽器配置文件中執(zhí)行Selenium命令。

當(dāng)然你也可以脫離 Selenium WebDriver 和 Puppeteer 自動化框架,可以直接調(diào)用我們的API來執(zhí)行自動化操作。

VMLogin結(jié)合使用puppeteer案例http://www.44651.cn/blog/1513.html

怎么控制IP地址轉(zhuǎn)換成WebSocket地址:http://www.44651.cn/blog/1577.html

支持的語言
Selenium框架提供了多種可搭配使用的語言,因此VMLogin防關(guān)聯(lián)瀏覽器自動化也可以在多種編碼語言上運行。但是目前,我們僅為Java和Python供技術(shù)支持。


VMLogin API token 是用戶的API令牌

這個需要用戶登陸到Web后臺,在帳號管理->我的帳戶,查看token。

此圖片的alt屬性為空;文件名為image-14.png

定義VMLogin端口:

您需要提前定義軟件端口以使用自動化。以下是定義端口的方法:

在軟件《我的帳戶》中打開啟用瀏覽器自動化設(shè)置,并在監(jiān)聽端口中設(shè)置能使用端口,這里默認是35000,另外你也可以設(shè)置一個訪問密碼。

隨后,您就可以通過定義的端口連接到VMLogin防關(guān)聯(lián)瀏覽器了。


VMLogin 本地 API 接口進行常規(guī)自動化操作
VMLogin自動化的網(wǎng)絡(luò)優(yōu)化,提升Window長時間自動化穩(wěn)定性的方法

VMLogin自動化測試工具

API多線程運行優(yōu)化工具,下載后放到安裝根目錄下即可。(功能是:API操作會在獨立進程里處理,緩解程序崩的情況)



接口還可以傳入代理服務(wù)器信息,如果傳入代理信息會覆蓋配置文件里的代理信息,這種覆蓋是臨時性的,不會真的修改配置文件,只對自動化接口有效:

http://127.0.0.1:35000/api/v1/profile/start?automation=true&profileId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&proxytype=socks5&proxyserver=ip&proxyport=1080&proxyusername=&proxypassword=

注意:接口有線上接口地址本地客戶端接口地址之分,兩個基地址不要混用:

C++

代理類型可能是這三種:
proxytype=socks5
proxytype=socks4
proxytype=http
proxytype=https

代理用戶名和密碼可以不傳為空。

Python 案例: (剛安裝 python ,先用 cmd 進 Python\Scripts 目錄,運行 pip install selenium? 和 pip install requests)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests

mla_profile_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
mla_url = 'http://127.0.0.1:35000/api/v1/profile/start?skiplock=true&profileId='+mla_profile_id

resp = requests.get(mla_url)
json = resp.json()

#判斷json status 返回狀態(tài) 如果是ERROR 終止進程并提示錯誤
errorcode='ERROR'

statuscode=json['status'] #json 返回狀態(tài)

if errorcode==statuscode:
    print(json['value']) #打印錯誤信息
    quit() #終止程序
    
print(json['value'])

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", json['value'][7:])
chrome_driver = r"chromedriver.exe" 
#http://chromedriver.storage.googleapis.com/92.0.4515.107/chromedriver_win32.zip
#下載 chromedriver 文件放到python目錄
driver = webdriver.Chrome(chrome_driver, options=chrome_options)

driver.get('https://www.bing.com/')
executor_url = driver.command_executor._url
session_id = driver.session_id
print(executor_url)
print(session_id)
print('ok it is done')

driver.quit()

Python 案例: (selenium 4 版本要使用以下的代碼,上面的代碼 在 selenium 4 中會報錯)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager


mla_profile_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
mla_url = 'http://127.0.0.1:35000/api/v1/profile/start?skiplock=true&profileId='+mla_profile_id

resp = requests.get(mla_url)
json = resp.json()


#判斷json status 返回狀態(tài) 如果是ERROR 終止進程并提示錯誤
errorcode='ERROR'

statuscode=json['status'] #json 返回狀態(tài)

if errorcode==statuscode:
    print(json['value']) #打印錯誤信息
    quit() #終止程序
    
print(json['value'])

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", json['value'][7:])

service = ChromeService(executable_path=json['chromedriver'])

driver = webdriver.Chrome( service=service ,options=chrome_options)

driver.get('https://www.bing.com/')
executor_url = driver.command_executor._url
session_id = driver.session_id
print(executor_url)
print(session_id)
print('ok it is done')

#driver.quit()
chromedriver 文件在每個內(nèi)核安裝目錄中都有,使用 API 啟努瀏覽器接口 /api/v1/profile/start 也會返回 chromedriver 文件全路徑位置。

selenium 防檢測可以在瀏覽器配置-》其它配置-》自定義啟動瀏覽器參數(shù) 里設(shè)置 --disable-blink-features=AutomationControlled   會起到一定做用。

注意一下VMLogin指紋瀏覽器內(nèi)核版本:

如果是90,就下載Python

http://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_win32.zip

如果是92,就下載Python

http://chromedriver.storage.googleapis.com/92.0.4515.107/chromedriver_win32.zip

如果是96,就下載Python

http://chromedriver.storage.googleapis.com/96.0.4664.45/chromedriver_win32.zip

如果是98,就下載Python

http://chromedriver.storage.googleapis.com/98.0.4758.80/chromedriver_win32.zip

chromedriver 版本不對應(yīng),會造成自動化失敗。

如果不能關(guān)閉瀏覽器,可以使用  http://127.0.0.1:35000/api/v1/profile/stop?profileId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  接口來關(guān)閉指定配置的瀏覽器進程。

如果你運行代碼只能打開瀏覽器,并沒有打開網(wǎng)站,那可能要把chromedriver.exe復(fù)制到你的 python 安裝目錄里了,還有一種可能是你選了移動仿真模式。

JAVA 案例:

package com.ruoyi.common.spider.reptile;

import cn.hutool.json.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author vmlogin
 *         <dependency>
 *             <groupId>org.seleniumhq.selenium</groupId>
 *             <artifactId>selenium-java</artifactId>
 *             <version>3.141.59</version>
 *         </dependency>
 */
public class ProductChrome {

    public static void main(String[] args) throws Exception {

       ProductChrome pc = new ProductChrome();
       String profileId = UUID.randomUUID().toString();

       //根據(jù)profileId打開并獲取遠程調(diào)試地址
      URL url = new URL(pc.startProfile("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));

      //使用遠程調(diào)試地址連接到打開的chrome瀏覽器
     System.setProperty("webdriver.chrome.driver", "D:\\VMLogin\\chrome\\90.0.4430.85\\chromedriver.exe");//指到驅(qū)動位置
     ChromeOptions chromeOptions = new ChromeOptions();
     chromeOptions.setExperimentalOption("debuggerAddress", url.getAuthority());
     WebDriver driver = new ChromeDriver(chromeOptions);


     //訪問vmlogin
     driver.get("https://www.bing.com/");
     System.out.println(driver.getTitle());
     driver.quit();

    }


    private String startProfile(String profileId) throws Exception {

        String url = "http://127.0.0.1:35000/api/v1/profile/start?automation=true&profileId=" + profileId;
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        JSONObject jsonResponse = new JSONObject(response.toString());
        return jsonResponse.getStr("value");
    }
}


VMLogin官方網(wǎng)站:http://www.44651.cn/