JAVA如何使用Firefox采集数据
文章目录
selenium是浏览器测试工具,可以模拟用户在浏览器中的各种操作。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。可以进行用户流程的录制(selenium IDE)、编写。在搭建环境的时候注意selenium和firefox的版本保持一致,同时设置开发者模式,防止被网站识别。
此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium,为了长期采集数据,还需要加上代理IP,保证采集效果
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);
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论