HttpClient爬虫-xx搜索出现验证码


前言

在XXapp输入关键字搜索,请求频繁时会出现验证码页面

更换新的cookie即可解决

在这里插入图片描述


代码

		// String url = "https:/xxx?type=2&s_from8&_A";
		String url = this.engineSite.getStartUrl();
		ArrayList<Article> list = new ArrayList<Article>();
		try {
			// 替换关键字
			url = url.replaceAll("ABC", URLEncoder.encode(keyword, "utf-8"));
			// 替换页数
			url = url.replace("CBA", pageorder + "");
			// jsoup获取doc对象
			Document doc = Jsoup.parse(new URL(url), 10000);
			// 必要时,重启cookie
			if (doc.getElementsByTag("ul").isEmpty()) {
				// 通过HttpClient得到Cookies
				HttpClient http = null;
				CookieStore httpCookieStore = new BasicCookieStore();
				http = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore).build();
				HttpGet httpRequest = new HttpGet("https://XXX.com/v?ie=utf8&query=&p=40030600");
				@SuppressWarnings("unused")
				HttpResponse httpResponse = null;
				try {
					httpResponse = http.execute(httpRequest);
				} catch (Exception e) {
					logger.error("HttpClient Connect Error :" + e);
				}
				List<Cookie> cookies = httpCookieStore.getCookies();
				
				// 设置cookie并发送请求
				HashMap<String, String> cookiesMap = new HashMap<String, String>();
				for (Cookie c : cookies) {
					cookiesMap.put(c.getName(), c.getValue());
				}

				doc = Jsoup.connect(url).cookies(cookiesMap).get();
			}

			// 根据li标签获取具体内容
			Elements Lis = doc.getElementsByTag("ul").get(1).getElementsByTag("li");

			// 循环生成文章对象
			for (int i = 0; i < Lis.size(); i++) {
				Article article = new Article();
				Element e = Lis.get(i);
				// 设置文章网站
				IWebsite web = new IWebsite();
				web.setWebUrl("https://xxxx.com/");
				web.setWebName("xxxn");
				article.setWebsite(web);
				// 设置url、author、content等
				article.setUrl(web.getWebUrl() + e.getElementsByAttribute("href").first().attr("href"));
				article.setAuthor(e.getElementById("XXX_11002601_account_" + i).text());
				article.setAuthorUrl(web.getWebUrl() + e.getElementById("zzz_vr_11002601_account_" + i).attr("href"));
				// 设置content为摘要
				article.setContent(article.getDigest());
				// 设置发布时间
				String dataTime = getWeixinTime(e.selectFirst("div.s-p").attr("t").toString());
				article.setPostTime(dataTime);
				// 设置标题
				article.setEngineName("XX");
				article.setSubject(doc.getElementsByTag("h3").get(i).text());
				// 设置唯一标识
				article.setUnicodeString(article.getUrl());

				list.add(article);
			}
		} catch (

		Exception e) {
			logger.error("Parse page Error :" + e);
		}
		return list;
上文代码中关键部分是重新拿取一个cookie发起请求。

效果

请添加图片描述
请添加图片描述

爬取上百行未出现验证码

最后

解决方案来自 https://blog.youkuaiyun.com/weixin_41186451/article/details/96980556
本帖仅供学习交流使用,禁止商用行为
既然你已经有了 `httpclient5-5.2.1.jar`,那么你还需要确保同时引入了它的核心依赖: --- ### ❗ 缺少的可能是:`httpcore5-5.2.1.jar` `HttpClient 5.x` 依赖于 `httpcore5` 模块。如果你只引入了 `httpclient5-5.2.1.jar`,但没有引入 `httpcore5-5.2.1.jar`,那么运行时仍然会报: ``` java.lang.NoClassDefFoundError: Could not initialize class org.apache.hc.client5.http.config.TlsConfig ``` 因为 `TlsConfig` 的实现依赖于 `httpcore5` 中的类。 --- ### ✅ 解决方案 #### 1. 确保你已下载并导入以下 JAR 包: | JAR 文件名称 | 用途说明 | |-----------------------------|---------| | `httpclient5-5.2.1.jar` | Apache HttpClient 5 的主库 | | `httpcore5-5.2.1.jar` | HttpClient 5 的核心依赖库 | 你可以在 Maven Central 手动下载它们: - [httpclient5-5.2.1.jar 下载地址](https://repo1.maven.org/maven2/org/apache/httpcomponents/client5/httpclient5/5.2.1/) - [httpcore5-5.2.1.jar 下载地址](https://repo1.maven.org/maven2/org/apache/httpcomponents/core5/httpcore5/5.2.1/) #### 2. 将这两个 JAR 添加到你的项目构建路径中 - **Eclipse**:右键项目 → `Build Path` → `Add External Archives` - **IntelliJ IDEA**:`File` → `Project Structure` → `Libraries` → 添加 JAR - **命令行运行**: ```bash javac -cp ".;httpclient5-5.2.1.jar;httpcore5-5.2.1.jar" YourClass.java java -cp ".;httpclient5-5.2.1.jar;httpcore5-5.2.1.jar" YourClass ``` #### 3. 测试代码示例 ```java import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.ClassicHttpResponse; public class TestHttpClient5 { public static void main(String[] args) throws Exception { try (CloseableHttpClient client = HttpClients.createDefault()) { HttpGet request = new HttpGet("https://example.com"); try (ClassicHttpResponse response = client.executeOpen(null, request, null)) { System.out.println("Response Code: " + response.getCode()); } } } } ``` --- ### ✅ 验证是否解决 运行程序后,如果没有再报 `NoClassDefFoundError`,说明问题已解决。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值