java利用Jsoup爬取网上免费代理IP,搭建自己的Ip池

package com.example.demo.util;

import com.beust.jcommander.internal.Lists;

import org.apache.commons.collections.CollectionUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.List;


public class FetchProxyIpUtil {



        public static List<String>  getProxyIp() throws IOException {
                List<String> proxyIpList = Lists.newArrayList();
                //解析网页(jsoup返回document就是浏览器document对象)
                Document document = Jsoup.parse(new URL("http://www.xiladaili.com/gaoni/"), 60000);
                Element body = document.selectFirst(".fl-table > tbody:nth-child(2)");
                Elements tr = body.getElementsByTag("tr");
                for(Element r : tr){
                        Elements td = r.getElementsByTag("td");
                        if(CollectionUtils.isNotEmpty(td)){
                                Element element = td.get(0);
                                String proxyIp = element.text();
                                proxyIpList.add(proxyIp);
                        }
                }
                return proxyIpList;
        }

        public static void main(String[] args) throws IOException {

                System.out.println(getProxyIp());
        }

}

 

返回结果:

[154.127.79.199:8080, 183.87.153.98:49602, 223.243.245.45:9999, 103.111.55.210:47623, 115.219.1.184:3256, 110.18.155.130:9999, 112.195.241.132:3256, 110.18.155.199:9999, 104.254.238.122:42415, 181.78.18.25:999, 183.148.226.161:3256, 139.224.211.212:8080, 124.112.171.132:9999, 103.253.113.54:443, 91.203.36.102:45551, 112.195.243.211:3256, 37.26.86.206:47464, 47.98.170.216:8080, 113.194.49.162:9999, 176.235.182.99:8080, 39.71.123.83:8118, 200.199.90.218:3128, 117.65.1.218:3256, 115.219.2.87:3256, 59.110.213.236:8118, 218.91.1.102:9999, 117.31.45.39:8888, 106.14.41.100:8080, 45.130.229.230:443, 45.64.11.1:8080, 157.230.103.91:37107, 112.195.241.196:3256, 112.195.240.117:3256, 104.254.238.122:8217, 58.176.147.14:8193, 136.228.128.236:8080, 182.253.107.210:8080, 36.91.108.138:3128, 112.195.240.231:3256, 131.161.237.113:8090, 202.150.138.234:8080, 90.189.110.170:3128, 202.61.99.26:3128, 195.53.49.11:3128, 103.83.178.166:8181, 103.135.225.10:3128, 187.243.253.2:8080, 175.110.211.166:8080, 116.68.170.190:8080, 157.100.53.108:999]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Jsoup爬取分页数据的步骤如下: 1. 定义要爬取的网页地址和需要爬取的内容。 2. 使用Jsoup连接网页,获取网页内容。 3. 使用Jsoup解析网页内容,提取需要的数据。 4. 如果网页中存在多页数据,使用循环遍历所有网页,重复步骤2和步骤3。 5. 将提取的数据保存到本地或数据库中。 下面是一个示例代码,演示如何使用Jsoup爬取分页数据: ```java import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class PageCrawler { public static void main(String[] args) throws IOException { // 定义要爬取的网页地址和需要爬取的内容 String url = "https://example.com/page/1"; String cssSelector = ".list-item"; // 使用Jsoup连接网页,获取网页内容 Document doc = Jsoup.connect(url).get(); // 使用Jsoup解析网页内容,提取需要的数据 Elements items = doc.select(cssSelector); for (Element item : items) { // 处理每个数据项 String title = item.select(".title").text(); String description = item.select(".description").text(); System.out.println(title); System.out.println(description); System.out.println("------------"); } // 如果网页中存在多页数据,使用循环遍历所有网页 for (int i = 2; i <= 10; i++) { String nextUrl = "https://example.com/page/" + i; doc = Jsoup.connect(nextUrl).get(); items = doc.select(cssSelector); for (Element item : items) { // 处理每个数据项 String title = item.select(".title").text(); String description = item.select(".description").text(); System.out.println(title); System.out.println(description); System.out.println("------------"); } } // 将提取的数据保存到本地或数据库中 // ... } } ``` 在示例代码中,我们首先定义了要爬取的网页地址和需要爬取的内容。 然后,我们使用Jsoup连接网页,获取网页内容,并使用Jsoup解析网页内容,提取需要的数据。 如果网页中存在多页数据,我们使用循环遍历所有网页,重复步骤2和步骤3。 最后,我们可以将提取的数据保存到本地或数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值