Skip to content

Instantly share code, notes, and snippets.

@koyachi
Forked from kui/scrape.dart
Last active April 7, 2018 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koyachi/d802c607223888651b5719a38fd9e4d8 to your computer and use it in GitHub Desktop.
Save koyachi/d802c607223888651b5719a38fd9e4d8 to your computer and use it in GitHub Desktop.
a web scraping script with Dart and html
import 'dart:io';
import 'dart:async';
import 'package:html/parser.dart';
import 'package:html/dom.dart';
main() {
final url = 'http://comic-walker.com/';
getHtml(url).then((document) {
print(document.querySelector('title').text);
document.querySelectorAll('#mainContent > li h2 span').forEach((e) {
print(e.text);
});
});
}
Future<Document> getHtml(String url) => new HttpClient()
.getUrl(Uri.parse(url))
.then((req) => req.close())
.then((res) =>
res.asyncExpand((bytes) => new Stream.fromIterable(bytes)).toList())
.then((bytes) => parse(bytes, sourceUrl: url));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment