爬蟲開發技巧

技術為輔,市場為王發表於2020-11-14

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
 
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;
 
public class FirefoxDriverProxyDemo
{
     / / 代理隧道驗證資訊
     final static String proxyUser = "username" ;
     final static String proxyPass = "password" ;
 
     / / 代理伺服器
     final static String proxyHost = "t.16yun.cn" ;
     final static int proxyPort = 31111 ;
 
     final static String firefoxBin = "C:/Program Files/Mozilla Firefox/firefox.exe" ;
 
     public static void main ( String[] args ) throws JSONException
     {
         System.setProperty ( "webdriver.firefox.bin" , firefoxBin ) ;
 
         FirefoxProfile profile = new FirefoxProfile ( ) ;
 
         profile.setPreference ( "network.proxy.type" , 1 ) ;
 
 
         profile.setPreference ( "network.proxy.http" , proxyHost ) ;
         profile.setPreference ( "network.proxy.http_port" , proxyPort ) ;
 
         profile.setPreference ( "network.proxy.ssl" , proxyHost ) ;
         profile.setPreference ( "network.proxy.ssl_port" , proxyPort ) ;
 
         profile.setPreference ( "username" , proxyUser ) ;
         profile.setPreference ( "password" , proxyPass ) ;
 
 
         profile.setPreference ( "network.proxy.share_proxy_settings" , true ) ;
 
 
         profile.setPreference ( "network.proxy.no_proxies_on" , "localhost" ) ;
 
 
         FirefoxDriver driver = new FirefoxDriver ( profile ) ;
     }
}

 
 
 
 

相關文章