selenium设置代理ip
来源:互联网 发布:网络短剧 搞笑 编辑:程序博客网 时间:2024/05/01 14:44
package com.fahai;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class SeleniumProxyIp {
private static String windowHandle;
public static void main(String[] args) throws InterruptedException {
//weile防止服务器封锁,这里的时间要模拟人的行为,随机且不能太短
long waitLoadBaseTime = 3000;
int waitLoadRandomTime = 3000;
Random random = new Random(System.currentTimeMillis());
//火狐浏览器
System.setProperty("webdriver.firefox.bin", "F:\\Firefox Setup 32.0.1\\firefox.exe");
//使用代理ip测试
String proxyIp = "139.59.211.210";
int proxyPort = 8080;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.ssl", proxyIp);
profile.setPreference("network.proxy.ssl_port", proxyPort);
//所有协议公用一种代理配置,如果单独配置,这项设置为false
profile.setPreference("network.proxy.share_proxy_settings", true);
WebDriver driver = new FirefoxDriver(profile);
// driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.get("http://www.yidianzixun.com/home?page=channel&keyword=%E8%8A%B1%E8%8C%B6");
//等待页面动态加载完毕
Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));
int pages = 1;
for(int i=1;i<2;i++){
driver.findElement(By.className("show-more")).click();
//等待页面加载完成
Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));
}
List<WebElement> findElements = driver.findElements(By.className("article"));
System.out.println("article的数量"+findElements.size());
WebElement webElement = findElements.get(0);
// for (WebElement webElement : findElements) {
WebDriver window;
//获取当前窗口句柄
windowHandle = driver.getWindowHandle();
WebElement link = webElement.findElement(By.tagName("h3")).findElement(By.tagName("a"));
System.out.println("link:"+link.getAttribute("href"));
//某个链接点击不到的原因
//1.未加载完成,增加等待时间
//2.被父节点隐藏,通过修改爬point(x.y)矢量积,使用链接能被模拟浏览器点击到
link.click();
//获得所有窗口的句柄
Set<String> handles = driver.getWindowHandles();
for (String string : handles) {
if (string.equals(windowHandle)) {
continue;
}else {
window = driver.switchTo().window(string);
window.manage().window().maximize();
window.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
window.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
//获取详细页面的内容
//因为有些链接是重定向直接跳转xpath格式不匹配
//抛出异常
try {
String text = window.findElement(By.xpath("//div[@class='content-hd-media']/h2")).getText();
System.out.println(text);
} catch (Exception e) {
e. printStackTrace();
}
//关闭窗口,其实也是该窗口句柄被rush
window.close();
}
//切换到主窗口
driver.switchTo().window(windowHandle);
}
Thread.sleep(1000);
driver.close();//关闭浏览器
// }
}
}
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class SeleniumProxyIp {
private static String windowHandle;
public static void main(String[] args) throws InterruptedException {
//weile防止服务器封锁,这里的时间要模拟人的行为,随机且不能太短
long waitLoadBaseTime = 3000;
int waitLoadRandomTime = 3000;
Random random = new Random(System.currentTimeMillis());
//火狐浏览器
System.setProperty("webdriver.firefox.bin", "F:\\Firefox Setup 32.0.1\\firefox.exe");
//使用代理ip测试
String proxyIp = "139.59.211.210";
int proxyPort = 8080;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.ssl", proxyIp);
profile.setPreference("network.proxy.ssl_port", proxyPort);
//所有协议公用一种代理配置,如果单独配置,这项设置为false
profile.setPreference("network.proxy.share_proxy_settings", true);
WebDriver driver = new FirefoxDriver(profile);
// driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.get("http://www.yidianzixun.com/home?page=channel&keyword=%E8%8A%B1%E8%8C%B6");
//等待页面动态加载完毕
Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));
int pages = 1;
for(int i=1;i<2;i++){
driver.findElement(By.className("show-more")).click();
//等待页面加载完成
Thread.sleep(waitLoadBaseTime+random.nextInt(waitLoadRandomTime));
}
List<WebElement> findElements = driver.findElements(By.className("article"));
System.out.println("article的数量"+findElements.size());
WebElement webElement = findElements.get(0);
// for (WebElement webElement : findElements) {
WebDriver window;
//获取当前窗口句柄
windowHandle = driver.getWindowHandle();
WebElement link = webElement.findElement(By.tagName("h3")).findElement(By.tagName("a"));
System.out.println("link:"+link.getAttribute("href"));
//某个链接点击不到的原因
//1.未加载完成,增加等待时间
//2.被父节点隐藏,通过修改爬point(x.y)矢量积,使用链接能被模拟浏览器点击到
link.click();
//获得所有窗口的句柄
Set<String> handles = driver.getWindowHandles();
for (String string : handles) {
if (string.equals(windowHandle)) {
continue;
}else {
window = driver.switchTo().window(string);
window.manage().window().maximize();
window.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
window.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
//获取详细页面的内容
//因为有些链接是重定向直接跳转xpath格式不匹配
//抛出异常
try {
String text = window.findElement(By.xpath("//div[@class='content-hd-media']/h2")).getText();
System.out.println(text);
} catch (Exception e) {
e. printStackTrace();
}
//关闭窗口,其实也是该窗口句柄被rush
window.close();
}
//切换到主窗口
driver.switchTo().window(windowHandle);
}
Thread.sleep(1000);
driver.close();//关闭浏览器
// }
}
}
0 0
- selenium设置代理ip
- 在selenium中设置代理ip
- selenium+python设置爬虫代理IP
- selenium加代理ip
- python selenium模块Chrome设置代理ip的方法
- 怎么样设置代理IP
- VC 设置代理IP
- 爬虫代理ip设置
- 爬虫代理ip设置
- java 设置代理ip
- java 设置代理IP
- 设置ip代理
- java代理IP设置实例
- 利用selenium时,chrome和firfox使用代理ip
- 【python 爬虫】selenium爬虫模块 phantomjs如何加代理IP
- Android模拟器代理设置、ip设置
- vb.net webbrowser控件 设置代理IP,webbrowser 代理IP
- selenium 代理
- 底部导航控件——BottomNavigationBar简单使用
- 常见小问题区别总结
- 问题 B: 笨鸟先飞
- 编写java程序151条建议读书笔记(1)
- 搭建Linux环境并软件安装(一)
- selenium设置代理ip
- C程序编译执行过程
- java 如何使的float保留2位或者多位小数
- 啊实打实的
- String中intern的方法
- SCOI2017酱油记
- nginx出现 “414 request-uri too large”
- 欢迎使用CSDN-markdown编辑器
- 学术笔记·总结·十六