White school Python reptiles (9): Reptile basis

Life is short, I used Python

The foregoing Portal:

White school Python Reptile (1): Opening

White Python crawler Science (2): Pre-preparation (a) is mounted substantially libraries

Getting Started with Linux pre-prepared base (B): white reptile learn Python (3)

Docker basis of pre-entry preparation (III): white reptile learn Python (4)

White school Python Reptile (5): pre-prepared (four) database infrastructure

White school Python Reptile (6): pre-prepared (E) crawler frame installation

White school Python reptiles (7): HTTP basic

White school Python reptiles (8): page basis

The core of reptiles

What is a reptile, practically speaking plain, reptiles that crawl web pages to extract information according to certain rules, repeat the above process is repeated to complete the automation program.

A reptile, the first thing is to crawl web pages, here mainly refers to the acquisition page's source code. In the page's source code, will contain the information we need, we need to do is extracted from the Source information.

When we request a web page, Python provides us with a lot of libraries to do it, such as urllib official, as well as requests, Aiohttp and other third parties.

We can use these libraries to send HTTP requests to retrieve data in response, the response obtained after, we only need to parse the data portion of the body where you can get the page source.

After obtaining the source code, our next job is to parse the source code to extract the data we need.

Extract data most basic and most common way is to use regular expressions, but this way is more complicated, but also more prone to error, but have to say, write a regular expression is very powerful people, do not need to complete the following these parsing library, which is a universal approach.

Quietly say, small series of regular expressions is not good to write, to only use these libraries provided by third parties.

It is used to extract data library Beautiful Soup, pyquery, lxml like. Using these libraries, we can quickly and efficiently extract information from the HTML page, such as attribute nodes, text, values, etc.

从源代码中提取到数据以后,我们会对数据进行保存,这里的保存形式多种多样,可以直接保存成 txt 、 json 、 Excel 文件等等,也可以保存至数据库,如 Mysql 、 Oracle 、 SQLServer 、 MongoDB 等等。

抓取的数据格式

一般而言,我们抓取到的都是 HTML 的网页源代码,这个是我们看得到的、常规的、直观的网页信息。

但是有些信息,并不是直接和 HTML 一起返回至网页的,会存在各种各样的 API 接口,这种接口返回的数据现在大多数是 JSON 的格式,也有一些会返回 XML 的数据格式,还会有一些个别的奇葩的接口直接返回程序猿自定义的字符串。这种 API 数据接口就需要具体问题具体分析了。

还有一些信息,比如各大图片站、视频站(如抖音、 B站),我们想要爬取的信息是图片或者视频,这些信息是已二进制的形式存在的,我们需要将这些二进制的数据爬取下来再进行转储。

此外,我们还能抓取到一些资源文件,如 CSS 、 JavaScript 等脚本资源,有的还会有一些 woff 等字体信息。这些信息是一个网页组成不可或缺的元素,只要浏览器能访问到的,我们都可以将其爬取下来。

现代前端页面爬取

今天核心内容来了!!!

很多时候,我们使用 HTTP 请求库爬取网页源代码时,爬取到的信息和我们在网页上看到的信息完全不一样,只有短短的几行。

这是因为最近这几年,前端技术突飞猛进,大量的采用前端模块化工具来构建前端页面,比较常用的框架有 Vue 、 React 等等。

导致我们获取到的网页只有一个空壳子,例如这种:

<!DOCTYPE html>
<html lang="en" style="background-color: #26282A; height: 100%">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title>演示项目</title>
  </head>
  <style>
    html,
    body,
    #app {
      height: 100%
    }
  </style>
  <body>
    <noscript>
      <strong>We're sorry but xxxxxx doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
    <script src=/js/chunk-vendors.84ee7bec.js></script>
    <script src=/js/app.4170317d.js></script>
  </body>
</html>

代码来源是博主平时做的一些小东西,其中博主已经省略大量引入的 JavaScript。

body 节点里面只有一个 id 为 app 的节点,但是需要注意在 body 节点的最后引入了 JavaScript 文件,它们负责整个网页的渲染。

在浏览器打开这个页面后,首先会加载这个 HTML 的内容,接着会发现有 JavaScript 的脚本文件加载,获取到这些脚本文件后,开始执行其中的代码,而 JavaScript 脚本文件则会修改整个页面的 HTML 代码,向其中添加节点,从而完成整个页面的渲染。

但是当我们使用请求库去请求这个页面的时候,只能获得当前的 HTML 的内容,它并不会去帮我们获取这个 JavaScript 脚本文件并且帮我们执行这个脚本文件渲染整个 HTML DOM 节点,我们当然也就看不到浏览器当中看到的内容。

这也解释了为什么有时我们得到的源代码和浏览器中看到的不一样。

当然,遇到这种情况也不要慌,我们还可以使用Selenium、Splash这样的库来实现模拟浏览器中的 JavaScript 渲染。

后面,我们会慢慢聊这些内容,本文主要先帮各位同学对爬虫有一个基础的了解,方便后续的学习。

参考:

https://cuiqingcai.com/5484.html

Guess you like

Origin www.cnblogs.com/babycomeon/p/11974612.html