曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Flutter

Run Crawlers on Your Phone! A First Look at a Flutter Crawler Framework~

Recently I needed a lot of data to train models for my graduation project. Many sites also have anti-crawling mechanisms, so I have to crawl for a while then pause, which makes it extremely slow. Running a crawler on my computer 24 hours a day would use too much electricity, so I thought of using my phone~ Then I thought of Flutter—old Android and Apple phones can all be put to use for data collection. ok, then I started building. After a day of tinkering I put together a rough framework. Finally I can conveniently add crawler tasks. It integrates basic features like config, logging, and data persistence, then wraps network requests and webpage parsing. It works reasonably well. Except, as everyone knows, dart

曦远 (DealiAxy)约 10 分钟
目录6 个章节

Recently I needed a lot of data to train models for my graduation project. Many sites also have anti-crawling mechanisms, so I have to crawl for a while then pause, which makes it extremely slow. Running a crawler on my computer 24 hours a day would use too much electricity, so I thought of using my phone~

Then I thought of Flutter—old Android and Apple phones can all be put to use for data collection.

ok, then I started building. After a day of tinkering I put together a rough framework. Finally I can conveniently add crawler tasks. It integrates basic features like config, logging, and data persistence, then wraps network requests and webpage parsing. It works reasonably well.

Except, as everyone knows, Dart is a single-threaded language. Although it provides an isolate mechanism, it can't share memory, so using it feels like having your hands tied. And Flutter even disabled reflection! The Google team is really something. After an afternoon of struggling I got so pissed off by this piece of junk that I simply abandoned the project.

If I want to implement a multi-threaded crawler, it looks like I'd have to write some native code anyway, in which case I might as well go fully native...

(ps: Why use this junk Flutter for crawling when perfectly good Android native is sitting there?? I must have gone nuts. So pissed off.)

App Screenshots

System Architecture

Drew a few diagrams

The SpiderTask base class. All crawler classes derive from this class. SpiderTask itself maintains a TaskConfig task configuration object and a logging object. As shown below:

As for data persistence, I used another class with the singleton pattern, initialized when the app starts.

Workflow

  1. Register crawler tasks in the task list on the home page
  2. After launching the app, manage each crawler task on the main page
  3. Select a task and start it
  4. Entering the detail page automatically binds to the crawler task's logging object so you can see log output

A Simple Example

Here's a simple example. When run, it crawls news from the CN Radio website.

import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_spider_fx/framework/index.dart';

class CNRadioNewsSpider extends SpiderTask {
  var url = 'http://news.cnr.cn/';

  CNRadioNewsSpider(BuildContext context) : super(context, 'CNRadioNewsSpider');

  @override
  start() async {
    super.start();
    var dom = await CatHttp.getDocument(url, encoding: 'gb2312');
    var links = dom.querySelectorAll('.contentPanel .lh30 a');
    links.forEach((link) {
      logging.debug(link.attributes['href']);
      logging.info(link.text);
    });
  }
}

Framework Code

For more details, you can check out the project code on GitHub~

Project URL: https://github.com/Deali-Axy/flutter_spider_fx

One last thing: junk Flutter. Don't blindly worship Google's tech. This thing is still a trap right now. Fine for writing a simple GUI to do CRUD, but don't even think about advanced stuff.

Welcome to Chat

For questions, please leave a message on the WeChat official account backend. I'll reply to every message~

_EOF_

Run Crawlers on Your Phone! A First Look at a Flutter Crawler Framework~

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

从细节入手改善用户体验,Flutter跨平台App开发中设置Android和iOS的启动页

前言 flutter开发App方便快捷,就是冷启动的时候要加载dart引擎有短暂的白屏,严重影响用户体验,这个时候我们就需要设置一个启动图片,像国内大部分App那样(例如微信那个地球),启动App的时候显示这个图片,加载完再进入App,不至于影响用户体验。 Android启动页 首先要准备好启动图,本文命名为splash.png,按照不同的分辨率添加到对应的mipmap-hdpi / mipmap

2021-12-18 · Flutter · 约 1 分钟

有模有样解决Flutter里Webview无法访问HTTP页面的问题

探索过程 Android9(好像是吧)开始谷歌就默认不让开发者访问不安全HTTP内容了,如果非要用HTTP,那必须在networkSecurityConfig里配置cleartextTrafficPermitted才行。谷歌这个设计的出发点本是好的,HTTPS固然更安全咱都懂,但有时候这个后端接口你选择不了要HTTP还是HTTPS,比如要使用没有提供HTTPS的第三方服务,或者是本文的这种情况:在

2021-12-18 · Flutter · 约 1 分钟

极简诗词app开发背后:Flutter移动应用快速构建实践——状态管理、国际化、数据持久化、性能优化(一)

前言 在开始之前的提示:虽然Flutter背靠Google这棵大树,但是毕竟还是一个年轻的技术,项目还处于初期阶段,更新非常快,问题也不是一般的多,使用Flutter的话就意味着必须忍受各种奇怪的bug和没有丰富中文资料的头疼,本文不是安利同学们入坑,只是对“极简诗词”app的开发过程进行记录。 另外app已经上架,有兴趣的同学可以下载试试:https://www.coolapk.com/apk/

2022-03-23 · Flutter · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…