记一次爬需要登录之后才能爬取数据的demo
一:工程概况

注意:
二:涉及到的类
package com.bigdata.crawler; import java.io.IOException;
import java.util.List; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions; import com.bigdata.util.DriverCommon; public class CnzzCrawler {
private String baseUrl ="http://new.cnzz.com/v1/login.php?siteid=1262437219";
private String password = "******";//查看密码 private ChromeDriver driver;
public CnzzCrawler() {
}
public CnzzCrawler(ChromeDriver driver) {
super();
this.driver = driver;
}
public void start(){
// 登入网站
driver.get(baseUrl);
// 输入密码
driver.findElement(By.id("password")).sendKeys(password); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 点击登入 html body div.pwdmain div.pwdcheck div.pwdcheck4 div form div img
// body > div.pwdmain > div.pwdcheck > div.pwdcheck4 > div:nth-child(1) > form > div:nth-child(2) > img driver.findElement(By.cssSelector("div.pwdcheck4 > div:nth-child(1) > form > div:nth-child(2) > img")).click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Document doc = Jsoup.parse(driver.getPageSource());
//System.out.println(doc);
//html.cnzz body div#userLoginHeader.userLoginHeader div.section div#rightContainer.rightContainer div#dashboardRootEl.dashboard ul#module_container.module.ui-sortable li.module_data0.moduleTwo table tbody tr.bg-white td.url div a.blue12
Elements elements= doc.select("a.blue12"); for(Element e: elements ){
String string = e.attr("href");
System.out.println(string);
}
driver.close();
}
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", DriverCommon.getDriverName(DriverCommon.getOSType()));
//System.setProperty("webdriver.firefox.driver", "D:/Program Files (x86)/Mozilla Firefox/firefox.exe"); ChromeDriver driver = new ChromeDriver();
//FirefoxDriver driver = new FirefoxDriver();
new CnzzCrawler(driver).start();
} }
package com.bigdata.util; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class DriverCommon {
/**
* 获取操作系统类型
*/
public static String getOSType(){
String temp = System.getProperty("os.name");
if(temp.contains("Mac")){
return "mac";
}else if(temp.contains("Win")){
return "win";
}else{
try {
Process process = Runtime.getRuntime().exec("getconf LONG_BIT");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = bufferedReader.readLine();
if(s.contains("64")){
return "linux64";
}else{
return "linux32";
}
} catch (IOException e) {
e.printStackTrace();
return "linux64"; //默认Linux64
}
}
} /**
* 获取浏览器驱动
* @param os
* @return
* @throws IOException
*/
public static String getDriverName(String os) throws IOException{
if(os == null)
return null;
switch (os) {
case "win":
return "chromedriver.exe";
case "mac":
return "chromedriver_mac";
case "linux_32":
return "chromedriver_linux32";
case "linux_64":
default:
return "chromedriver_linux64";
}
}
}
记一次爬需要登录之后才能爬取数据的demo的更多相关文章
- PHP Curl模拟登录并抓取数据
使用PHP的Curl扩展库可以模拟实现登录,并抓取一些需要用户账号登录以后才能查看的数据.具体实现的流程如下(个人总结): 1. 首先需要对相应的登录页面的html源代码进行分析,获得一些必要的信息: ...
- PHP获取cookie、Token、模拟登录、抓取数据、解析生成json
本文介绍使用PHP获取cookie,获取Token.以及模拟登录.然后抓取数据.最后解析生成json的的过程. 0. 设置Cookie路径 set_time_limit(0); //使用的cookie ...
- Python登录豆瓣并爬取影评
上一篇我们讲过Cookie相关的知识,了解到Cookie是为了交互式web而诞生的,它主要用于以下三个方面: 会话状态管理(如用户登录状态.购物车.游戏分数或其它需要记录的信息) 个性化设置(如用户自 ...
- Scrapy 通过登录的方式爬取豆瓣影评数据
Scrapy 通过登录的方式爬取豆瓣影评数据 爬虫 Scrapy 豆瓣 Fly 由于需要爬取影评数据在来做分析,就选择了豆瓣影评来抓取数据,工具使用的是Scrapy工具来实现.scrapy工具使用起来 ...
- 记一次HTTPClient模拟登录获取Cookie的开发历程
记一次HTTPClient模拟登录获取Cookie的开发历程 环境: springboot : 2.7 jdk: 1.8 httpClient : 4.5.13 设计方案 通过新建一个 ...
- 运用cookie登陆人人网爬取数据
浏览器访问WEB服务器的过程 在用户访问网页时,不论是通过URL输入域名或IP,还是点击链接,浏览器向WEB服务器发出了一个HTTP请求(Http Request),WEB服务器接收到客户端浏览器的请 ...
- python高级—— 从趟过的坑中聊聊爬虫、反爬以及、反反爬,附送一套高级爬虫试题
前言: 时隔数月,我终于又更新博客了,然而,在这期间的粉丝数也就跟着我停更博客而涨停了,唉 是的,我改了博客名,不知道为什么要改,就感觉现在这个名字看起来要洋气一点. 那么最近到底咋不更新博客了呢?说 ...
- Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...
- python模拟浏览器爬取数据
爬虫新手大坑:爬取数据的时候一定要设置header伪装成浏览器!!!! 在爬取某财经网站数据时由于没有设置Header信息,直接被封掉了ip 后来设置了Accept.Connection.User-A ...
随机推荐
- css 添加伪元素 消除浮动 对父元素高度产生的影响
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
Google IO 2017宣布了 Kotlin 会成为 Android 官方开发语言.一时间朋友圈和Android圈被各种刷屏.当然我也顺势而为发布了一篇的文章<为什么我要改用Kotlin&g ...
- PHP练习
<?php function table($row,$col,$c){ $str= "<table border=1>"; for ($i=0; $i <$ ...
- 模仿python中的range功能
主要是利用生成器来写的一个函数: def myxrange(start, stop = None, step = 1): #这里的stop一定要等于None,不能等于0,要不然会有好多问题 if st ...
- stm32寄存器版学习笔记05 PWM
STM32除TIM6和TIM7外都可以产生PWM输出.高级定时器TIM1和TIM8可以同时产生7路PWM,通用定时器可以产生4路PWM输出. 1.TIM1 CH1输出PWM配置步骤 ①开启TIM1时钟 ...
- POJ3696 The Windy's 【网络流】
#include<iostream> #include<vector> #include<cstring> #include<algorithm> #i ...
- P2P UPD打洞原理
转自:http://blog.pfan.cn/fengfei/18828.html 首先先介绍一些基本概念: NAT(Network Address Tr ...
- Nomad 了解
Introduction to Nomad Welcome to the intro guide to Nomad! This guide is the best place to start wit ...
- oracle11g安装(win10)
oracle11g安装(win10) 0.安装环境 1.安装包下载 1)http://www.oracle.com/technetwork/database/enterprise-edition/do ...
- 常用 Git 命令使用教程
下面整理一下自己在开发过程中经常使用到的 Git 命令.使用 GUI 工具的同学,也可以对照起来看看. Git 配置 1. 在安装完成 Git 后,开始正式使用前,是需要有一些全局设置的,如用户名.邮 ...