• java_网路爬虫_1


    网络爬虫介绍

    在大数据时代,信息的采集是一项重要的工作,而互联网中的数据是海量的, 如果单纯靠人力进行信息采集,不仅低效繁琐,搜集的成本也会提高。如何自动 高效地获取互联网中我们感兴趣的信息并为我们所用是一个重要的问题,而爬虫 技术就是为了解决这些问题而生的。 网络爬虫(Web crawler)也叫做网络机器人,可以代替人们自动地在互联网 中进行数据信息的采集与整理。它是一种按照一定的规则,自动地抓取万维网信 息的程序或者脚本,可以自动采集所有其能够访问到的页面内容,以获取或更新 这些网站的内容和检索方式。 从功能上来讲,爬虫一般分为数据采集,处理,储存三个部分。爬虫从一个或 若干初始网页的 URL 开始,获得初始网页上的 URL,在抓取网页的过程中,不断 从当前页面上抽取新的 URL 放入队列,直到满足系统的一定停止条件。

    为什么学网络爬虫

    我们初步认识了网络爬虫,但是为什么要学习网络爬虫呢?只有清晰地知道我 们的学习目的,才能够更好地学习这一项知识。在此,总结了 4 种常见的学习爬 虫的原因: 1. 可以实现搜索引擎 我们学会了爬虫编写之后,就可以利用爬虫自动地采集互联网中的 信息,采集回来后进行相应的存储或处理,在需要检索某些信息的时候, 只需在采集回来的信息中进行检索,即实现了私人的搜索引擎。

    2. 大数据时代,可以让我们获取更多的数据源。 在进行大数据分析或者进行数据挖掘的时候,需要有数据源进行分 析。我们可以从某些提供数据统计的网站获得,也可以从某些文献或内 部资料中获得,但是这些获得数据的方式,有时很难满足我们对数据的 需求,而手动从互联网中去寻找这些数据,则耗费的精力过大。此时就 可以利用爬虫技术,自动地从互联网中获取我们感兴趣的数据内容,并 将这些数据内容爬取回来,作为我们的数据源,再进行更深层次的数据 分析,并获得更多有价值的信息。

    3. 可以更好地进行搜索引擎优化(SEO)。 对于很多 SEO 从业者来说,为了更好的完成工作,那么就必须要对 搜索引擎的工作原理非常清楚,同时也需要掌握搜索引擎爬虫的工作原 理。 而学习爬虫,可以更深层次地理解搜索引擎爬虫的工作原理,这样 在进行搜索引擎优化时,才能知己知彼,百战不殆。

    4. 有利于就业。 从就业来说,爬虫工程师方向是不错的选择之一,并且随着大数据时代和人工智能的来临,爬虫 技术的应用将越来越广泛,在未来会拥有很好的发展空间。

    HttpClient

    网络爬虫就是用程序帮助我们访问网络上的资源,我们一直以来都是使 用 HTTP 协议访问互联网的网页,网络爬虫需要编写程序,在这里使用同样 的 HTTP 协议访问网页。 这里我们使用 Java 的 HTTP 协议客户端 HttpClient 这个技术,来实现抓 取网页数据。

    不带参数的GET 请求爬取数据

    1. package cn.lala.crawler.httpclient.test;
    2. import org.apache.http.client.methods.CloseableHttpResponse;
    3. import org.apache.http.client.methods.HttpGet;
    4. import org.apache.http.impl.client.CloseableHttpClient;
    5. import org.apache.http.impl.client.HttpClients;
    6. import org.apache.http.util.EntityUtils;
    7. import java.io.IOException;
    8. @SuppressWarnings("all")
    9. public class HttpClientGetTest {
    10. public static void main(String[] args) {
    11. CloseableHttpClient httpClient = HttpClients.createDefault();
    12. //不带参数的get请求
    13. HttpGet httpGet = new HttpGet("https://www.baidu.com/");
    14. CloseableHttpResponse response = null;
    15. try {
    16. response = httpClient.execute(httpGet);
    17. if (response.getStatusLine().getStatusCode() == 200) {
    18. String html = EntityUtils.toString(response.getEntity(), "UTF-8");
    19. System.out.println("请求成功,数据的长度是:"+html.length());
    20. }
    21. } catch (IOException e) {
    22. throw new RuntimeException(e);
    23. } finally {
    24. if (response != null) {
    25. try { //关闭response
    26. response.close();
    27. //关闭httpClient
    28. httpClient.close();
    29. } catch (IOException e) {
    30. throw new RuntimeException(e);
    31. }
    32. }
    33. }
    34. }
    35. }

    带参数的 GET 请求

    1. package cn.lala.crawler.httpclient.test;
    2. import org.apache.http.client.methods.CloseableHttpResponse;
    3. import org.apache.http.client.methods.HttpGet;
    4. import org.apache.http.client.utils.URIBuilder;
    5. import org.apache.http.impl.client.CloseableHttpClient;
    6. import org.apache.http.impl.client.HttpClients;
    7. import org.apache.http.util.EntityUtils;
    8. import java.io.IOException;
    9. import java.net.URI;
    10. import java.net.URISyntaxException;
    11. @SuppressWarnings("all")
    12. public class HttpClientGetParamTest {
    13. public static void main(String[] args) throws URISyntaxException {
    14. CloseableHttpClient httpClient = HttpClients.createDefault();
    15. //带参数的get请求
    16. //HttpGet httpGet = new HttpGet("https://www.baidu.com/s?wd=java");
    17. //创建URIBuilder来构建参数
    18. URIBuilder uriBuilder = new URIBuilder("https://www.baidu.com/s").setParameter("wd", "java");
    19. URI uri = uriBuilder.build();
    20. System.out.println(uri);
    21. HttpGet httpGet = new HttpGet(uri);
    22. CloseableHttpResponse response = null;
    23. try {
    24. response = httpClient.execute(httpGet);
    25. if (response.getStatusLine().getStatusCode() == 200) {
    26. String html = EntityUtils.toString(response.getEntity(), "UTF-8");
    27. System.out.println("请求成功,数据的长度是:" + html.length());
    28. }
    29. } catch (IOException e) {
    30. throw new RuntimeException(e);
    31. } finally {
    32. if (response != null) {
    33. try { //关闭response
    34. response.close();
    35. //关闭httpClient
    36. httpClient.close();
    37. } catch (IOException e) {
    38. throw new RuntimeException(e);
    39. }
    40. }
    41. }
    42. }
    43. }

    不带参数的POST 请求爬取数据

    1. package cn.lala.crawler.httpclient.test;
    2. import org.apache.http.client.methods.CloseableHttpResponse;
    3. import org.apache.http.client.methods.HttpGet;
    4. import org.apache.http.client.methods.HttpPost;
    5. import org.apache.http.impl.client.CloseableHttpClient;
    6. import org.apache.http.impl.client.HttpClients;
    7. import org.apache.http.util.EntityUtils;
    8. import java.io.IOException;
    9. @SuppressWarnings("all")
    10. public class HttpClientPostTest {
    11. public static void main(String[] args) {
    12. CloseableHttpClient httpClient = HttpClients.createDefault();
    13. //创建HttpPost对象,不带请求参数的post请求
    14. HttpPost httpPost = new HttpPost("https://www.oschina.net/");
    15. //设置请求头,解决网站反爬
    16. httpPost.setHeader("User-Agent","");
    17. CloseableHttpResponse response = null;
    18. try {
    19. response = httpClient.execute(httpPost);
    20. if (response.getStatusLine().getStatusCode() == 200) {
    21. String html = EntityUtils.toString(response.getEntity(), "UTF-8");
    22. System.out.println("请求成功,数据的长度是:"+html.length());
    23. }
    24. } catch (IOException e) {
    25. throw new RuntimeException(e);
    26. } finally {
    27. if (response != null) {
    28. try { //关闭response
    29. response.close();
    30. //关闭httpClient
    31. httpClient.close();
    32. } catch (IOException e) {
    33. throw new RuntimeException(e);
    34. }
    35. }
    36. }
    37. }
    38. }

    带参数的 POST 请求

    1. package cn.lala.crawler.httpclient.test;
    2. import org.apache.http.NameValuePair;
    3. import org.apache.http.client.entity.UrlEncodedFormEntity;
    4. import org.apache.http.client.methods.CloseableHttpResponse;
    5. import org.apache.http.client.methods.HttpPost;
    6. import org.apache.http.impl.client.CloseableHttpClient;
    7. import org.apache.http.impl.client.HttpClients;
    8. import org.apache.http.message.BasicNameValuePair;
    9. import org.apache.http.util.EntityUtils;
    10. import java.io.IOException;
    11. import java.io.UnsupportedEncodingException;
    12. import java.util.ArrayList;
    13. @SuppressWarnings("all")
    14. public class HttpClientPostParamTest {
    15. public static void main(String[] args) throws UnsupportedEncodingException {
    16. CloseableHttpClient httpClient = HttpClients.createDefault();
    17. //创建HttpPost对象,带参数的post请求
    18. HttpPost httpPost = new HttpPost("https://www.oschina.net/");
    19. //设置请求头,解决网站反爬
    20. httpPost.setHeader("User-Agent","");
    21. //创建List集合封装请求参数
    22. ArrayList pairs = new ArrayList<>();
    23. //使用封装请求参数的List集合,
    24. pairs.add(new BasicNameValuePair("scope","project"));
    25. pairs.add(new BasicNameValuePair("q","httpClient"));
    26. // 创建表单实体对象
    27. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs,"UTF-8");
    28. //把表单实体对象设置到post请求中
    29. httpPost.setEntity(formEntity);
    30. CloseableHttpResponse response = null;
    31. try {
    32. response = httpClient.execute(httpPost);
    33. if (response.getStatusLine().getStatusCode() == 200) {
    34. String html = EntityUtils.toString(response.getEntity(), "UTF-8");
    35. System.out.println("请求成功,数据的长度是:"+html.length());
    36. }
    37. } catch (IOException e) {
    38. throw new RuntimeException(e);
    39. } finally {
    40. if (response != null) {
    41. try { //关闭response
    42. response.close();
    43. //关闭httpClient
    44. httpClient.close();
    45. } catch (IOException e) {
    46. throw new RuntimeException(e);
    47. }
    48. }
    49. }
    50. }
    51. }

  • 相关阅读:
    GitHub 最全的开发资源汇总系列
    leetcode经典面试150题---2.移除元素
    代理SSL证书的优势——JoySSL
    [C/C++]数据结构----顺序表的实现(增删查改)
    spring集成web环境-ContextLoaderListener监听分析
    【目标检测竞赛总结】IEEE UV 2022 “Vision Meets Algae” Object Detection Challenge
    【嵌入式开发学习】__单片机中容易造成内存泄露的几个痛点
    [C++ 网络协议] 多播与广播
    精彩回顾|关系网络赋能银行数字化转型的应用与实践
    Selenium4+Python3系列(十一) - Page Factory设计模式
  • 原文地址:https://blog.csdn.net/weixin_44478828/article/details/134289709