当前位置: 首页 > news >正文

Java爬虫----HttpClient方式(获取数据篇)

目录

一、爬虫的定义

二、获取数据

(1)基于Get方式的请求(无参)

(2)基于Get方式请求(有参)

(3)基于Post方式的请求(无参)

(4)基于Post方式的请求(有参)


一、爬虫的定义

爬虫指的是一种自动化程序,能够模拟人类在互联网上的浏览行为,自动从互联网上抓取、预处理并保存所需要的信息。


爬虫运行的过程一般是先制定规则(如指定要抓取的网址、要抓取的信息的类型等),紧接着获取该网址的HTML源代码,根据规则对源代码进行解析和抽取,最后进行处理和保存。

爬虫在实际应用中广泛使用,如搜索引擎、大数据分析、交易数据采集等领域,都需要用到爬虫技术来实现信息的定向采集和处理

关于爬虫,我们基本上可以分为两步,第一是获取数据,第二是解析数据;

二、获取数据

(1)基于Get方式的请求(无参)

public static void main(String[] args) throws IOException {CloseableHttpClient httpClient = HttpClients.createDefault();HttpGet httpGet=new HttpGet("https://www.lanqiao.cn/");//发送http中的get请求HttpEntity entity=null;CloseableHttpResponse response=null;//判断是否得到正确的数据try {response= httpClient.execute(httpGet);if(response.getStatusLine().getStatusCode()==200){//获取响应数据entity=response.getEntity();//获取的数据输出其实是个对象System.out.println(entity);//将响应数据以html源码形式展示String html = EntityUtils.toString(entity, "UTF-8");System.out.println(html);}}catch (Exception e){e.printStackTrace();}finally {try{if(response!=null)response.close();//响应成功后关闭if(httpClient!=null)httpClient.close();}catch(Exception e){e.printStackTrace();}}}

(2)基于Get方式请求(有参)

public static void main(String[] args) throws IOException {CloseableHttpClient httpClient = HttpClients.createDefault();HttpGet httpGet=new HttpGet("https://www.lanqiao.cn/");//发送http中的get请求HttpEntity entity=null;CloseableHttpResponse response=null;//判断是否得到正确的数据try {response= httpClient.execute(httpGet);if(response.getStatusLine().getStatusCode()==200){//获取响应数据entity=response.getEntity();//获取的数据输出其实是个对象System.out.println(entity);//将响应数据以html源码形式展示String html = EntityUtils.toString(entity, "UTF-8");System.out.println(html);}}catch (Exception e){e.printStackTrace();}finally {try{if(response!=null)response.close();//响应成功后关闭if(httpClient!=null)httpClient.close();}catch(Exception e){e.printStackTrace();}}}

(3)基于Post方式的请求(无参)

public class HtppClientDemo1 {public static void main(String[] args) throws IOException {CloseableHttpClient httpClient=HttpClients.createDefault();//创建post请求HttpPost httpPost=new HttpPost("https://www.lanqiao.cn/");HttpEntity entity=null;CloseableHttpResponse response=null;try{response=httpClient.execute(httpPost);if(response.getStatusLine().getStatusCode()==200){//获取响应数据entity=response.getEntity();System.out.println(entity);//网页源代码String html=EntityUtils.toString(entity,"UTF-8");System.out.println(html);}}catch(Exception e){e.printStackTrace();}finally {try{if(response!=null)response.close();if(httpClient!=null)httpClient.close();}catch (Exception e){e.printStackTrace();}}}

(4)基于Post方式的请求(有参)

public static void main(String[] args) {CloseableHttpClient httpClient = HttpClients.createDefault();//创建post请求HttpPost httpPost = new HttpPost("https://www.lanqiao.cn/");HttpEntity entity = null;CloseableHttpResponse response = null;try {//设置参数BasicNameValuePair basicNameValuePair=new BasicNameValuePair("progid","20");//装入集合List<BasicNameValuePair> list=new ArrayList<>();list.add(basicNameValuePair);//开始进行参数请求,进行网络请求UrlEncodedFormEntity urlEncodedFormEntity=new UrlEncodedFormEntity(list,"UTF-8");httpPost.setEntity(urlEncodedFormEntity);//请求参数结束response = httpClient.execute(httpPost);if (response.getStatusLine().getStatusCode() == 200) {//获取响应数据entity = response.getEntity();System.out.println(entity);//网页源代码String html = EntityUtils.toString(entity, "UTF-8");System.out.println(html);}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) response.close();if (httpClient != null) httpClient.close();} catch (Exception e) {e.printStackTrace();}}}

http://www.lryc.cn/news/105097.html

相关文章:

  • 计算机视觉实验:图像增强应用实践
  • ES6:Generator函数详解
  • 前端小练-产品宣传页面
  • arm学习之stm32设备树学习-中断控制led灯亮灭+字符设备指令控制led灯亮灭
  • 快速开发框架若依的基础使用详解
  • RabbitMQ 教程 | 第4章 RabbitMQ 进阶
  • 小程序如何从分类中移除商品
  • P1219 [USACO1.5] 八皇后 Checker Challenge
  • 如何在不使用脚本和插件的情况下手动删除 3Ds Max 中的病毒?
  • SpringCloud Gateway 在微服务架构下的最佳实践
  • Android studio修改app图标
  • <C++> 三、内存管理
  • 大模型开发(十五):从0到1构建一个高度自动化的AI项目开发流程(上)
  • HarmonyOS 开发基础(二)组件拼凑简单登录页面
  • flutter minio
  • ChatGPT:人工智能交互的新时代
  • C. Binary String Copying - 思维
  • 哈工大计算机网络课程网络安全基本原理详解之:密钥分发中心与公钥认证中心
  • md5sum
  • 图文档数字化:实现高效管理的几大步骤
  • 服务器磁盘占用过高分析
  • 【C语言】通讯录3.0 (文件存储版)
  • 【C#常用操作】
  • 深入理解CountDownLatch计数器
  • 从SQL注入绕过最新安全狗WAF中学习fuzz
  • C语言每日一题:12《数据结构》相交链表。
  • 【Spring框架】SpringMVC
  • HDFS中namenode安全模式
  • blender凹凸感和置换形变
  • 力扣 343. 整数拆分