[置顶] 网络爬虫:利用Selenium实现登录
2015-11-17 08:09
746 查看
转载链接:http://www.tongtongxue.com/archives/180.html
写过爬虫程序的码农都知道,实现爬虫程序登录的方法有多种,我这利用Selenium来实现登录。提供源代码下载
本案例实现登录的网站是iteye,同时登录时选择第三方登录工具,本处利用的新浪微博来登录。
以下是关键代码:
程序启动类:WebSpider.java
package com.tongtongxue.webspider;
import com.tongtongxue.webspider.fetcher.Fetcher;
public class WebSpider {
public static void main(String[] args) throws Exception {
Fetcher fetcher = new Fetcher();
fetcher.fetch();
}
}
抓取类:Fetcher.java
package com.tongtongxue.webspider.fetcher;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
public class Fetcher {
private WebDriver webDriver;
private Selenium selenium;
// 第三方的登录工具的用户名,我这里
7f32
用的是新浪微博
private String username = "xxxxxx";
// 第三方的登录工具的密码,我这里用的是新浪微博
private String password = "xxxxxx";
public Fetcher() throws Exception {
// 设置google浏览器的驱动位置
System.setProperty("webdriver.chrome.driver" , "E:/it_jar_file/chromedriver_win32/chromedriver.exe");
webDriver = new ChromeDriver();
webDriver.manage().timeouts().pageLoadTimeout(12000, TimeUnit.SECONDS);
selenium = new WebDriverBackedSelenium(webDriver, "http://www.iteye.com");
selenium.setTimeout("120000");
}
public void fetch() throws Exception {
// 这是入口网址
webDriver.get("http://www.iteye.com/login");
waitForPageToLoad();
// 选择第三方登录工具
selenium.click("css=div.third a[href='/auth/weibo']");
waitForPageToLoad();
// 输入用户名
webDriver.findElement(By.id("userId")).sendKeys(username);
// 输入密码
webDriver.findElement(By.id("passwd")).sendKeys(password);
Thread.sleep(2000);
// 点击登录按钮
selenium.click("css=p.oauth_formbtn a[node-type='submit']");
}
private void waitForPageToLoad() {
selenium.waitForPageToLoad("120000");
}
}
写过爬虫程序的码农都知道,实现爬虫程序登录的方法有多种,我这利用Selenium来实现登录。提供源代码下载
本案例实现登录的网站是iteye,同时登录时选择第三方登录工具,本处利用的新浪微博来登录。
以下是关键代码:
程序启动类:WebSpider.java
package com.tongtongxue.webspider;
import com.tongtongxue.webspider.fetcher.Fetcher;
public class WebSpider {
public static void main(String[] args) throws Exception {
Fetcher fetcher = new Fetcher();
fetcher.fetch();
}
}
抓取类:Fetcher.java
package com.tongtongxue.webspider.fetcher;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
public class Fetcher {
private WebDriver webDriver;
private Selenium selenium;
// 第三方的登录工具的用户名,我这里
7f32
用的是新浪微博
private String username = "xxxxxx";
// 第三方的登录工具的密码,我这里用的是新浪微博
private String password = "xxxxxx";
public Fetcher() throws Exception {
// 设置google浏览器的驱动位置
System.setProperty("webdriver.chrome.driver" , "E:/it_jar_file/chromedriver_win32/chromedriver.exe");
webDriver = new ChromeDriver();
webDriver.manage().timeouts().pageLoadTimeout(12000, TimeUnit.SECONDS);
selenium = new WebDriverBackedSelenium(webDriver, "http://www.iteye.com");
selenium.setTimeout("120000");
}
public void fetch() throws Exception {
// 这是入口网址
webDriver.get("http://www.iteye.com/login");
waitForPageToLoad();
// 选择第三方登录工具
selenium.click("css=div.third a[href='/auth/weibo']");
waitForPageToLoad();
// 输入用户名
webDriver.findElement(By.id("userId")).sendKeys(username);
// 输入密码
webDriver.findElement(By.id("passwd")).sendKeys(password);
Thread.sleep(2000);
// 点击登录按钮
selenium.click("css=p.oauth_formbtn a[node-type='submit']");
}
private void waitForPageToLoad() {
selenium.waitForPageToLoad("120000");
}
}
相关文章推荐
- 解决百度地图找不到SDKInitializer,无法解析的问题
- apach+https安装过程出现的问题
- linux 下部署 openssl+apache 配置https
- 【计算机网络】:IP分片详解及例题
- BP神经网络的简单字符识别算法
- TCP协议中的三次握手和四次挥手
- telnet的网络协商
- RESTful 接口规范
- iOS 两种方法获取当前网络状态
- Android之用HttpURLConnection参数以XML形式封装的部分关键代码
- TCP、UDP 和Socket编程
- java网络编程之路(一)
- 网络编程-SOCKET选项-缓冲区
- 基于http的QQ协议代码
- netstat 查看TCP状态值
- 网络编程最佳实践
- ///判断网络状态 (简单版)
- HTTP2 学习
- HDU3549 网络流入门题(Dinic)
- 聊聊HTTPS和SSL/TLS协议