reqularList.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import 'package:video/model/video.dart';
  2. import 'request.dart';
  3. class Regular {
  4. static getHttp(String url) async {
  5. var responseBody = await Request.getByHttpHtml(url);
  6. List<Video> recommends = getRecommend(responseBody);
  7. return recommends;
  8. }
  9. static List<Video> getRecommend(String responseBody) {
  10. RegExp reg = new RegExp(r'class="xing_vb">[\s\S]*?</div');
  11. Iterable<Match> matches = reg.allMatches(responseBody);
  12. List<Video> list = [];
  13. for (Match m in matches) {
  14. //groupCount返回正则表达式的分组数
  15. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  16. String match = m.group(0);
  17. //在此匹配
  18. list = getItems(match);
  19. }
  20. return list;
  21. }
  22. static List<Video> getItems(String match) {
  23. RegExp reg = new RegExp(
  24. r'class="xing_vb4">[\s\S]*?href="(.*?)"[\s\S]*?target="_blank">(.*?)</a[\s\S]*?class="xing_vb5">(.*?)</span[\s\S]*?class="xing_vb6">(.*?)</span[\s\S]*?/li>');
  25. Iterable<Match> matches = reg.allMatches(match);
  26. List<Video> list = [];
  27. for (Match m in matches) {
  28. //groupCount返回正则表达式的分组数
  29. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  30. String url = "";
  31. String title = "";
  32. String time = "";
  33. for (int i = 0; i < m.groupCount + 1; i++) {
  34. String match = m.group(i);
  35. switch (i) {
  36. case 1:
  37. url = match;
  38. break;
  39. case 2:
  40. title = match;
  41. break;
  42. case 3:
  43. break;
  44. case 4:
  45. time = match;
  46. break;
  47. }
  48. }
  49. Video video = new Video(url: url, title: title, time: time);
  50. list.add(video);
  51. }
  52. return list;
  53. }
  54. }