• 爬虫:用selenium+phantomJS实现简单的登录破解,本文以博客园的登录为例


    有时候大家在爬虫的时候会遇到要登录的情况,如果不登录则爬不到自己想要的东西,这里以博客园为例,here we go~~

    首先简单的介绍一下selenium和phantomJS:

    selenium是一款测试工具,能够模拟用户对浏览器进行操作,

    phantomJS是一款轻便式浏览器,其没有界面并且功能相对简单,因此速度上会比较快速。

    说到这大家应该明白selenium+phantomJS如何实现登录破解了,对,就是模拟人工操作。

    我们现看一下博客园的登录界面:https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F

     

    找到这些元素所在的点,填写账号密码,并且点击登录按钮,之后等待一段时间,因为页面加载需要时间,之后就可以爬取登陆后的界面:

    话不多说,直接上代码:

    package com.eversec.crawler;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriverService;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    /**
     * Created by tyx on 2017/9/14.
     * 通过selenium+phantomJS登录博客园
     */
    public class CnblogLogin {
        public static void main(String[] args) throws Exception{
            DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
            //ssl证书支持
            desiredCapabilities.setCapability("acceptSslCerts", true);
            //截屏支持
            desiredCapabilities.setCapability("takesScreenshot", true);
            //css搜索支持
            desiredCapabilities.setCapability("cssSelectorsEnabled", true);
            //js支持
            desiredCapabilities.setJavascriptEnabled(true);
            //驱动支持
            desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"D:\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe");
            //创建无界面浏览器对象
    
            WebDriver webDriver = new PhantomJSDriver(desiredCapabilities);
            try {
                webDriver.get("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F");
                WebElement usernameEle = webDriver.findElement(By.id("input1"));
                WebElement passwordEle = webDriver.findElement(By.id("input2"));
    //        设置账号密码
                usernameEle.sendKeys("");
                passwordEle.sendKeys("");
                WebElement loginButtom = webDriver.findElement(By.id("signin"));
                loginButtom.click();
                webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                List<WebElement> elements = webDriver.findElements(By.xpath("//a[@class='titlelnk']"));
                for (WebElement element : elements){
                    System.out.println(element.getText());
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                webDriver.close();
                webDriver.quit();
            }
        }
    }

    运行后结果如下:

    由此实现对博客园登录的破解

    欢迎大家来交流爬虫方面的经验

    人生苦短,远离IT脱离苦海
  • 相关阅读:
    System.TypeInitializationException 类型初始值设定项引发异常
    asp.net webapi下json传值方式
    The remote name could not be resolved: 'nuget.org'(未能解析此远程名称:’nuget.org’)
    关于集成Paypal Express Checkout支付功能
    Syntax error at line 16 while loading: expected ')', got keyword 'in' or(i.isArray(t)||(t in e?t=[t]:(t=i.came
    如何在MVC3 razor视图下的ViewsStart文件中设置使用两套不同的Layout布局视图
    knockout使用技巧:告知knockout忽略页面容器内特定部分的绑定
    LINQ to Entities已知问题及注意事项
    jQuery中.live()方法的使用方法
    Uncaught TypeErroe: Uncaught TypeError: Cannot call method 'push' of undefined 和 Uncaught TypeError: undefined is not a function
  • 原文地址:https://www.cnblogs.com/liuxiaopang/p/7527448.html
Copyright © 2020-2023  润新知