Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Forked from StrykerKKD/main.dart
Created December 10, 2018 14:15
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 h4ck4life/1333f263aaa178db44aeb5cb7d36ad75 to your computer and use it in GitHub Desktop.
Save h4ck4life/1333f263aaa178db44aeb5cb7d36ad75 to your computer and use it in GitHub Desktop.
Web scraping with Dart 2
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as parser;
import 'package:html/dom.dart';
main() async {
http.Response response = await http.get('https://news.ycombinator.com/');
Document document = parser.parse(response.body);
await for (Element element in tagStream(document,'a')){
print(element.text);
}
}
Stream tagStream(Document document, String tag) async*{
for(Element element in document.getElementsByTagName(tag)){
yield element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment