使用 dart 的网页抓取工具

时间:2021-05-18 18:27:48

标签: html css flutter dart

我正在尝试为单个网站创建一个网络抓取工具,只挑选一个标题、图片和网站链接。 标题很好,但图像和链接无法正常工作,任何人都可以帮我解决这个问题。这是我在 .yaml 中使用的代码和依赖项

import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> title;
  List<String> image;
  List<String> link;

  @override
  void initState() {
    _getdataFromWeb();
  }

  void _getdataFromWeb() async {
    final response = await http.get('https://www.bewakoof.com/men-shirts');
    dom.Document document = parser.parse(response.body);
    final pictures = document.getElementsByClassName('productGrid');
    final description = document.getElementsByClassName('productCardDetail');
    final nextPage =
      //document.getElementsByClassName('entry-title');
      document.getElementsByClassName('col-sm-4 col-xs-6');

    image = pictures
      .map((element) =>
          element.getElementsByTagName("img")[0].attributes['src'])
      .toList();
    title = description
      .map((element) => element.getElementsByTagName("h3")[0].innerHtml)
      .toList();
    link = nextPage
      .map((element) => element.getElementsByTagName("a")[0].attributes['href'])
      .toList();
    print(link);
  }

  @override
  Widget build(BuildContext context) {
    print("hello");
    if (image == null)
      print("null");
    else
      print(image);
    return SafeArea(
      child: Scaffold(
      backgroundColor: Colors.black87,
      body: title == null || title.length == 0
        ? Text(
            "No data",
            style: TextStyle(
              color: Colors.white,
            ),
          )
        : ListView.builder(
            itemCount: title.length,
            itemBuilder: (context, index) {
              return AnimationConfiguration.staggeredList(
                position: index,
                duration: const Duration(milliseconds: 375),
                child: SlideAnimation(
                  child: FadeInAnimation(
                    child: GestureDetector(
                      onTap: () async {
                        dynamic url = link[index];
                        if (await canLaunch(url))
                          launch(url);
                        else {
                          print('error');
                        }
                      },
                      child: Padding(
                        padding: const EdgeInsets.all(10),
                        child: Card(
                          child: Container(
                            color: Colors.black87,
                            child: Column(
                              children: <Widget>[
                                Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    title[index],
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      color: Colors.cyan,
                                      fontSize: 20,
                                    ),
                                  ),
                                ),
                                SizedBox(height: 15),
                                Image.network(image[0]),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              );
            },
          ),
      ),
    );
  }
}
cupertino_icons: ^1.0.1
http: ^0.12.0+4
html: ^0.14.0+3
flutter_staggered_animations: "^0.1.2"
url_launcher: ^5.4.0

如果可能的话,我明天可能需要它

1 个答案:

答案 0 :(得分:0)

enter image description here

在这里,我为元素其余部分添加了 html 图片,我只调试了链接不起作用。 以下是调试代码:

import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> title;
  List<String> image;
  List<String> link;

  @override
  void initState() {
    _getdataFromWeb();
}

      void _getdataFromWeb() async {
    final response = await http.get(
        'https://www.bewakoof.com/');
    dom.Document document = parser.parse(response.body);
    final pictures =
        document.getElementsByClassName('chWrprInner');
    //document.getElementsByClassName('entry-header blog-entry-header');
final description =
    //document.getElementsByClassName('entry-content');
    document.getElementsByClassName('chWrprInner');
final nextPage =
    //document.getElementsByClassName('entry-title');
    document.getElementsByClassName('chWrprInner');

image = pictures
    .map((element) =>
          element.getElementsByTagName("img")[0]
          .attributes['src'])
      .toList();
  title = description
      .map((element) => element.getElementsByTagName("p")[0]
          .innerHtml)
       .toList();
  link = nextPage
      .map((element) =>
          element.getElementsByTagName("a")[0]
          .attributes['href'])
       .toList();
  print(link);
  }

  @override
  Widget build(BuildContext context) {
print("hello");
if (image == null)
  print("null");
else
  print(image);
return SafeArea(
  child: Scaffold(
    backgroundColor: Colors.black87,
    body: title == null || title.length == 0
        ? Text(
            "No data",
            style: TextStyle(
              color: Colors.white,
            ),
          )
        : ListView.builder(
            itemCount: title.length,
            itemBuilder: (context, index) {
              return AnimationConfiguration.staggeredList(
                position: index,
                duration: const Duration(milliseconds: 375),
                child: SlideAnimation(
                    child: FadeInAnimation(
                        child: GestureDetector(
                  onTap: () async {
                    dynamic url = link[index];
                    if (await canLaunch(url))
                      launch(url);
                    else {
                      print('error');
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(10),
                    child: Card(
                      child: Container(
                        color: Colors.black87,
                        child: Column(
                          children: <Widget>[
                            Align(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                title[index],
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  color: Colors.cyan,
                                  fontSize: 20,
                                ),
                              ),
                            ),
                            SizedBox(height: 15),
                            Text(
                                 title[index],
                                 style: TextStyle(
                               color: Colors.white,
                              ),
                            ),
                            Image.network(image[index]),
                          ],
                        ),
                      ),
                    ),
                  ),
                ))),
              );
            },
          ),
  ),
);
}

}

相关问题