网络爬虫:利用Selenium实现登录
来源:互联网 发布:组态软件 电力行业 编辑:程序博客网 时间:2024/05/16 05:39
转载链接: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;
- // 第三方的登录工具的用户名,我这里用的是新浪微博
- 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");
- }
- }
阅读全文
1 0
- 网络爬虫:利用Selenium实现登录
- 网络爬虫:利用Selenium实现登录
- Selenium 实现的网络爬虫
- 利用python实现网络爬虫
- 利用selenium写微博爬虫
- 利用selenium进行爬虫
- android利用爬虫实现模拟登录
- 利用HttpClient、HtmlParser简单实现网络爬虫
- 利用nodeJS实现的网络小爬虫
- Selenium 实现爬虫
- selenium 实现爬虫
- python 爬虫 利用selenium模拟登录帐号 向requests中重设 cookie
- selenium java实现登录
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
- 利用selenium实现截图
- Python爬虫获取cookie:利用selenium
- 网络爬虫:利用有道实现“语言翻译”功能
- 从零开始学网络爬虫之selenium/phantomjs
- 设计一个学生类Student和它的一个子类Undergraduate
- TDD 测试驱动开发
- solr学习添加文档(Add Document)
- Jackson之JSON包的使用分析
- 非常齐全的链表操作
- 网络爬虫:利用Selenium实现登录
- 文档总结3-Linux中man命令详解与用法
- Vegas如何调整亮度和对比度?(调色篇)
- oracle之查询语句2
- 设计一个Shape接口和它的两个实现类Square和Circle
- 个人投资感悟
- PXE自动安装linux系统原理(一)
- 【开发也是好测试】(四)—Mock
- robots文件的优化