Java开发笔记Ⅱ(Jsoup爬虫)

2024-06-19 18:12
文章标签 java 开发 笔记 爬虫 jsoup

本文主要是介绍Java开发笔记Ⅱ(Jsoup爬虫),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Jsoup 爬虫

Java 也能写爬虫!!!

Jsoup重要对象如下:

Document:文档对象,每个html页面都是一个Document对象

Element:元素对象,一个Document对象里有多个Element对象

Node:节点对象,用于存储数据,标签名称、属性都是节点对象

Jsoup的主要方法如下:

static Connection connect(String url) 创建URL连接

static Document parse(File in, String charsetName) 解析文件为 Document 对象

static Document parse(String html) 解析html代码为 Document 对象

(虽然上边是最主要的方法,但是下边这段代码中,是用 document对象 + css 选择器来获取的信息)

爬虫示例(豆瓣)

   /*** 通过访问接口获取代理IP*/public void initIPPool() {System.out.println("开始获取IP...");Process proc;try {// 这个代码之前是python改的,这里偷懒直接调用,这个文件贴在后边proc = Runtime.getRuntime().exec("python getIP.py");BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));String line = null;while ((line = in.readLine()) != null) {System.out.println(line);}in.close();proc.waitFor();} catch (Exception e) {System.out.println(e.toString());}System.out.println("成功获取代理IP");}/*** 从存储代理IP的文件获取代理IP*/public void loadIPPool() {File file = new File("ipPool.txt");List<String> list = new ArrayList<String>();synchronized (this) {BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(file));String tempString = null;// 一次读入一行,直到读入null为文件结束while ((tempString = reader.readLine()) != null) {list.add(tempString);}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {System.out.println(e1.toString());}}}}System.out.println(list);myIPPool = list.toArray(new String[list.size()]);System.out.println("成功载入IP代理池");}public String crawlOnce(Integer start) {StringBuilder finalResult = new StringBuilder();Random random = new Random();// 请求地址String url ="http://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=" +(start);HttpGet request = new HttpGet(url);String proxyIp = myIPPool[random.nextInt(myIPPool.length)];while (proxyIp.split(":").length != 2) {// 在代理ip池里随机获取一个ipproxyIp = myIPPool[random.nextInt(myIPPool.length)];}HttpHost proxy = new HttpHost(proxyIp.split(":")[0],Integer.parseInt(proxyIp.split(":")[1]));SSLContextBuilder builder = new SSLContextBuilder();// 全部信任 不做身份鉴定PoolingHttpClientConnectionManager cm = null;SSLConnectionSocketFactory sslsf = null;try {builder.loadTrustMaterial(null, new TrustStrategy() {@Overridepublic boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {return true;}});sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello","SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);Registry<ConnectionSocketFactory> registry =RegistryBuilder.<ConnectionSocketFactory>create().register("http",new PlainConnectionSocketFactory()).register("https", sslsf).build();cm = new PoolingHttpClientConnectionManager(registry);cm.setMaxTotal(200);//max connection} catch (Exception e) {System.out.println(e.toString());return "";}//设置认证CredentialsProvider provider = new BasicCredentialsProvider();//第一个参数对应代理httpHost,第二个参数设置代理的用户名和密码,如果代理不需要用户名和密码,填空provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("", ""));//实例化CloseableHttpClient对象CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm).setConnectionManagerShared(true).setDefaultCredentialsProvider(provider).build();RequestConfig config = RequestConfig.custom().setProxy(proxy).setConnectTimeout(CONNECTION_TIME_OUT).setConnectionRequestTimeout(CONNECTION_TIME_OUT).setSocketTimeout(CONNECTION_TIME_OUT).build();request.setConfig(config);//添加请求头request.addHeader("User-Agent", myUAPool[random.nextInt(myUAPool.length)]);request.addHeader("Cookie", myCookies[random.nextInt(myCookies.length)]);request.addHeader("Accept-Language", "zh-CN,zh;q=0.9");request.addHeader("Sec-Fetch-Mode", "cors");request.addHeader("Sec-Fetch-Site", "same-origin");HttpResponse response = null;BufferedReader rd = null;try {response = httpClient.execute(request);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));} catch (IOException e) {logError(start);return "";}String line = "";StringBuilder result = new StringBuilder();while (true) {try {line = rd.readLine();if (line == null) {break;}} catch (IOException e) {logError(start);break;}// 请求返回了html页面if (line.equals("") || line.charAt(0) == '<') {break;}result.append(line);}System.out.println((start) + "--result:" + result);JSONObject res = JSONObject.parseObject(String.valueOf(result));if (res == null || !res.containsKey("data")) {logError(start);return "";}JSONArray jsonArray = res.getJSONArray("data");for (int i = 0; i < jsonArray.size(); i++) {JSONObject jo = jsonArray.getJSONObject(i);// 通过详情链接爬取电影详情finalResult.append(crawlDetails(jo.getString("url")));}return finalResult.toString();}// 爬取详情private String crawlDetails(String url) {String result = "";Random random = new Random();try {String proxyIp = myIPPool[random.nextInt(myIPPool.length)];// myUAPool这里可以换几个浏览器把useragent手写在变量里Connection con = Jsoup.connect(url).proxy(proxyIp.split(":")[0], Integer.parseInt(proxyIp.split(":")[1])).timeout(10000).userAgent(myUAPool[random.nextInt(myUAPool.length)]).header("Accept-Language", "zh-CN,zh;q=0.9").header("Cookie", myCookies[random.nextInt(myCookies.length)]).timeout(CONNECTION_TIME_OUT); // 设置连接超时时间// 执行连接,获取页面Connection.Response response = con.execute();Document document = con.get();String info = document.select("#info").text();// IDresult += url.substring(33, url.length() - 1);// 标题result += "," + document.select("#content > h1 > span:nth-child(1)").text();// 年份result += "," + document.select("#content > h1 > span.year").text();// 导演result += "," + document.select("#info > span:nth-child(1) > span.attrs > a").text();// 编剧result += "," + document.select("#info > span:nth-child(3) > span.attrs").text();// 主演result += "," + document.select("#info > span.actor > span.attrs").text();// 类型result += "," + document.select("[property=v:genre]").text();// 产地result += "," + info.substring(info.indexOf("制片国家/地区: "), info.indexOf(" 语言:")).substring("制片国家/地区: ".length());// 语言if (info.contains(" 上映日期:")) {result += "," + info.substring(info.indexOf("语言: "), info.indexOf(" 上映日期:")).substring("语言: ".length());} else {result += "," + info.substring(info.indexOf("语言: ")).substring("语言: ".length());}// 片长result += "," + document.select("[property=v:genre]").attr("content");// 评分result += "," +document.select("#interest_sectl > div > div.rating_self.clearfix > strong").text();// 5result += "," + document.select("#interest_sectl > div.rating_wrap.clearbox > div" +".ratings-on-weight > div:nth-child(1) > span" +".rating_per").text();// 4result += "," + document.select("#interest_sectl > div.rating_wrap.clearbox > div" +".ratings-on-weight > div:nth-child(2) > span" +".rating_per").text();// 3result += "," + document.select("#interest_sectl > div.rating_wrap.clearbox > div" +".ratings-on-weight > div:nth-child(3) > span" +".rating_per").text();// 2result += "," + document.select("#interest_sectl > div.rating_wrap.clearbox > div" +".ratings-on-weight > div:nth-child(4) > span" +".rating_per").text();// 1result += "," + document.select("#interest_sectl > div.rating_wrap.clearbox > div" +".ratings-on-weight > div:nth-child(5) > span" +".rating_per").text();// 评分人数result += "," + document.select("[property=v:votes]").text();// 评论数result +="," + document.select("#comments-section > div.mod-hd > h2 > span > a").text();System.out.println(proxyIp + " " + result);} catch (IOException e) {System.out.println(e.toString());}return result + "\n";}

获取代理IP的代码

# coding=UTF-8import requests
import jsonclass FreeIP():def __init__(self):# 代理ip网站self.url = "http://proxylist.fatezero.org/proxy.list"self.headers = {"User-Agent": "这里改为浏览器的useragent"}def check_ip(self, ip_list):correct_ip = []for ip in ip_list:if len(correct_ip) > 10:  # 可以根据自己的需求进行更改或者注释掉breakip_port = "{}:{}".format(ip["host"], ip["port"])proxies = {'https': ip_port}try:# 如果请求该网址,返回的IP地址与代理IP一致,则认为代理成功response = requests.get('https://icanhazip.com/', proxies=proxies,timeout=3).text  # 可以更改timeout时间if response.strip() == ip["host"]:# print("可用的IP地址为:{}".format(ip_port))correct_ip.append(ip_port)except:# print("不可用的IP地址为:{}".format(ip_port))return correct_ipdef run(self):response = requests.get(url=self.url).content.decode()ip_list = []proxies_list = response.split('\n')for proxy_str in proxies_list:try:proxy = {}proxy_json = json.loads(proxy_str)if proxy_json["anonymity"] == "high_anonymous" and proxy_json["type"] == "https":host = proxy_json['host']port = proxy_json['port']proxy["host"] = hostproxy["port"] = portip_list.append(proxy)except:correct_ip = self.check_ip(ip_list)file_path = 'ipPool.txt'# 写入这个文件with open(file_path, mode='w', encoding='utf-8') as file_obj:for i in correct_ip:file_obj.write(i + "\n")if __name__ == '__main__':ip = FreeIP()ip.run()

这篇关于Java开发笔记Ⅱ(Jsoup爬虫)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1075766

相关文章

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接

java连接opcua的常见问题及解决方法

《java连接opcua的常见问题及解决方法》本文将使用EclipseMilo作为示例库,演示如何在Java中使用匿名、用户名密码以及证书加密三种方式连接到OPCUA服务器,若需要使用其他SDK,原理... 目录一、前言二、准备工作三、匿名方式连接3.1 匿名方式简介3.2 示例代码四、用户名密码方式连接4

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Java中的record使用详解

《Java中的record使用详解》record是Java14引入的一种新语法(在Java16中成为正式功能),用于定义不可变的数据类,这篇文章给大家介绍Java中的record相关知识,感兴趣的朋友... 目录1. 什么是 record?2. 基本语法3. record 的核心特性4. 使用场景5. 自定

如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socket read timed out的问题

《如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socketreadtimedout的问题》:本文主要介绍解决Druid线程... 目录异常信息触发场景找到版本发布更新的说明从版本更新信息可以看到该默认逻辑已经去除总结异常信息触发场景复

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3