Giter Site home page Giter Site logo

spider's Introduction

爬虫项目

这是一个前后端全栈项目。

  1. 后端使用Dart实现,爬取个人博客网站数据,为前端提供相应API接口。
  2. 前端使用Flutter实现,展示后端爬取到的数据。

Dart后端项目创建

需要使用Dart SDK提供的命令行工具创建项目目录结构,对应的命令为dart。使用dart --help查看所有可使用的命令。

其中dart create用以创建项目,使用dart help create查看其使用帮助,及所有可用参数。-t表示指定项目类型,这里要创建命令行工具应用,则使用console参数值。

故,有Dart后端项目创建命令,如下:

dart create dart_spider -t console

生成的项目目录结构,如下:

dart_spider
├── CHANGELOG.md
├── README.md
├── analysis_options.yaml
├── bin
│   └── dart_spider.dart
├── lib
│   └── dart_spider.dart
├── pubspec.lock
├── pubspec.yaml
└── test
    └── dart_spider_test.dart

运行项目:

dart bin/dart_spider.dart

添加Dart项目依赖

在pubspec.yaml文件中添加Dart项目远程库依赖。在vs code中保存文件自动拉取依赖,或在Dart项目根目录路径下使用dart pub get命令。

  1. http。用作网络请求库。
  2. html。用以解析http语法。

爬虫类设计

所谓爬虫,是用代码自动化访问网页。关键在于模拟用户访问行为,欺骗浏览器。主要有2个入参。

  1. user-agent。伪造用户身份。整个应用内不变,设为成员变量。
  2. url。所要访问的目标网页地址,设为类的实例方法入参。

将入参作为json格式配置文件,与源代码分隔开,通过Dart SDK读取文件,再decode为Map<String, String>格式的json表示。

爬虫实现原理

  1. 发起Http请求,获取Response响应数据。
  2. 解析Html语法,主要通过CSS选择器或正则。

具体实现注意点

  1. Html解析时部分元素可能为空,要判空。
  2. 构造爬虫数据实体类,进行数据封装。
  3. 分页数据批量获取。
  4. 集成持久化方案。

集成Sqlite3

编译Sqlite3源码

./configure
make
make install

spider's People

Contributors

fengwensheng avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.