记一次爬需要登录之后才能爬取数据的demo

news/2024/7/19 9:28:22 标签: 爬虫, python, java

一:工程概况

注意:

 

二:涉及到的类

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";
		}
	}
}

  

 

转载于:https://www.cnblogs.com/ipetergo/p/7102290.html


http://www.niftyadmin.cn/n/1650816.html

相关文章

ubuntu14.04上引入thinkphp5类库遇到的一个问题

ubuntu14.04 上加载OSS\OssClient() ;--->在vendor文件夹下的文件要用大写OSS 小写的报错 无法加载类库 Vendor(OSS.autoload);//引入文件$accessKeyId ""; $accessKeySecret "";$endpoint "";try {$ossClient new \OSS\OssClient($acces…

go java .net_Go和Java的性能对比,真的如此吗?

前两天我看到了一篇文章,测试Java和Go和Python的性能,其测试内容是一个排序,排序一亿次,然后看那个语言耗时最短,我先贴一下这个文章的测试结果,Java竟然比Go快了一倍不止,Go不是号称接近C的性能…

java tostring null_ToStringBuilder不打印null解决办法

一、问题描述:经过一段时间查看linux上日志,发现日志里面大量打印JAVA对象是null的情况,遇到到业务问题时,常常需要通过对象的当时快照去定位问题,这时需要消耗很多时间去查找对应的关键对象属性字段数据是否准确&…

XtraBackup构建MySQL主从环境的方法

环境:HE3主库,HE1从库HE1:192.168.1.248HE3:192.168.1.250从库my.cnf加入以下参数并重启数据库:read_only1log_slave_updates1(这里推荐开启,从库记录binlog)主库授权备份账户mysql>grant SELECT,RELOAD…

java 字符串动态创建对象_通过字符串动态创建类对象

通过字符串动态创建类对象功能的需求是 不需要使用IOC 框架 来对创建指定对象。.NET 中 要想动态的 创建对象 做耦合 好像只能做到 将类生成DLL 后 然后 加载DLL 接着创建对象。JAVA 当个类文件 就可以运行。希望做到 当读一个 .class 或者 .TXT 就可以 动态创建类对…

UVA 11090 Going in Cycle!!

题意 给你一个有向图,问你定义一个环的平均值为这个环上所有边的平均值,问你最小的环的平均值是多少。 做法 1. 二分答案。 2. 如果平均数为mid;则若这个环的每条边长度是x1~xn,就有(x1-mid)&#xff0…

Java文件操作(IO流)

File 在java中用来表示文件和文件夹的具体类。 File的构建 File的常见方法 字节流:       对象序列化: 字符流: 转接流: InputStreamReader、OutputStreamReader。 将字节流转成字符流。主要是为了网络部分服务&#xff0…

java数据修改_用Java实现数据的显示,增加,删除,修改

先声明一点,是一个刚学Javase的新手,有许多不足,希望大家见谅和建议。代码1.ShopStockManage.javaimport java.util.ArrayList;import java.util.Scanner;/** 1.商品数据初始化* 2.用户选择菜单* 3 选择不同的功能* 3.1 Read* …