宝安网站制作需要多少钱,个人主页推荐,一般网站建设方案,报名网站辽宁省建设银行目录
一、爬虫的定义
二、获取数据
#xff08;1#xff09;基于Get方式的请求#xff08;无参#xff09;
#xff08;2#xff09;基于Get方式请求#xff08;有参#xff09;
#xff08;3#xff09;基于Post方式的请求#xff08;无参#xff09;
…
目录
一、爬虫的定义
二、获取数据
1基于Get方式的请求无参
2基于Get方式请求有参
3基于Post方式的请求无参
4基于Post方式的请求有参 一、爬虫的定义 爬虫指的是一种自动化程序能够模拟人类在互联网上的浏览行为自动从互联网上抓取、预处理并保存所需要的信息。 爬虫运行的过程一般是先制定规则如指定要抓取的网址、要抓取的信息的类型等紧接着获取该网址的HTML源代码根据规则对源代码进行解析和抽取最后进行处理和保存。 爬虫在实际应用中广泛使用如搜索引擎、大数据分析、交易数据采集等领域都需要用到爬虫技术来实现信息的定向采集和处理 关于爬虫我们基本上可以分为两步第一是获取数据第二是解析数据 二、获取数据
1基于Get方式的请求无参 public static void main(String[] args) throws IOException {CloseableHttpClient httpClient HttpClients.createDefault();HttpGet httpGetnew HttpGet(https://www.lanqiao.cn/);//发送http中的get请求HttpEntity entitynull;CloseableHttpResponse responsenull;//判断是否得到正确的数据try {response httpClient.execute(httpGet);if(response.getStatusLine().getStatusCode()200){//获取响应数据entityresponse.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 httpGetnew HttpGet(https://www.lanqiao.cn/);//发送http中的get请求HttpEntity entitynull;CloseableHttpResponse responsenull;//判断是否得到正确的数据try {response httpClient.execute(httpGet);if(response.getStatusLine().getStatusCode()200){//获取响应数据entityresponse.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 httpClientHttpClients.createDefault();//创建post请求HttpPost httpPostnew HttpPost(https://www.lanqiao.cn/);HttpEntity entitynull;CloseableHttpResponse responsenull;try{responsehttpClient.execute(httpPost);if(response.getStatusLine().getStatusCode()200){//获取响应数据entityresponse.getEntity();System.out.println(entity);//网页源代码String htmlEntityUtils.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 basicNameValuePairnew BasicNameValuePair(progid,20);//装入集合ListBasicNameValuePair listnew ArrayList();list.add(basicNameValuePair);//开始进行参数请求进行网络请求UrlEncodedFormEntity urlEncodedFormEntitynew 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();}}}