记一次爬需要登录之后才能爬取数据的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 ...
随机推荐
- PostgreSQL备份工具-pg_backrest(转)
转自:http://blog.chinaunix.net/uid-7270462-id-5777877.html 官网:https://pgbackrest.org 一.配置集中备份服务器 1.1 备 ...
- 天气预报api整理
目录 1.国家气象局提供的天气预报接口 1.1.接口网址 1.2.示范代码 2.各个城市对应的代码 作为一个开发的diaosi,在开发过程中,时常会遇到添加天气的功能,现在百忙之中抽出一点时间整理了一 ...
- keras系列︱keras是如何指定显卡且限制显存用量
keras在使用GPU的时候有个特点,就是默认全部占满显存. 若单核GPU也无所谓,若是服务器GPU较多,性能较好,全部占满就太浪费了. 于是乎有以下三种情况: - 1.指定GPU - 2.使用固定显 ...
- img 在chrome和Firefox下的兼容性
Firefox 5.下:没有重叠在一起: chrome 下: 两个重叠在一起, 原因: img标签,在两个浏览器下展示效果不一样, 解决办法:指明展示:
- DevOps实践
云计算 [ 01 ]GitLab+Rancher实践DevOps[转载] [ 02 ]Kubernetes CentOS 7.4搭建Kubernetes 1.8.5集群 kubeadm搭建kubern ...
- Git详解之七 自定义Git
以下内容转载自:http://www.open-open.com/lib/view/open1328070404827.html自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式, ...
- Centos kvm+ceph
Centos kvm+ceph 一. centos6.5 安装kvm 1. disable selinux 2. 确认支持intel虚拟化 3. 安装需要的包 4.设置桥接网络 5.运行kvm ins ...
- threeJs中旋转位移等操作坐标系
1.场景中的立方体.圆柱等三维模型的位置属性.平移方法参照Scene对象的世界坐标系 2.场景中立方体.圆柱等的三维模型的角度属性.旋转方法参照模型自身的模型坐标系
- ubuntu查看进程端口号及运行的程序
查看进程端口号及运行的程序:netstat -atunp 根据端口号来查看进程号:lsof -i:16031
- 《DSP using MATLAB》示例Example 8.27
%% ------------------------------------------------------------------------ %% Output Info about thi ...