将信息从HTML保存到列表中

时间:2019-09-21 16:55:13

标签: html flutter web-scraping dart

我想从我的大学教授中列出一个清单。我从网站(这是一个wordpress网站,所以我稍微使用了URL)中获取信息。

我从创建列表的女巫创建了一个ProfesorItem类

class ProfesorItem {
  String name;
  String tip;
  String photo;
  String email;
  String web;
  String adress;
  String domainsOfInterest;

  ProfesorItem({
    this.name,
    this.tip,
    this.photo,
    this.email,
    this.web,
    this.adress,
    this.domainsOfInterest,
  });
}
List<ProfesorItem> profesoriList = new List<ProfesorItem>();

我用Json从网站上获取的数据只是身体内容,而RegEx则将其分成字符串列表,每个字符串都有关于每个Professor的信息。这里是一个例子。

<img class="alignnone size-full wp-image-17243" src="http://www.cs.ubbcluj.ro/wp-content/uploads/Andreica-Anca-133x100.jpg" alt="Andreica Anca" width="100" height="133" /></div>
安德里亚·安卡(Andreica Anca)博士,教授大学
电子邮件:anca [at] cs.ubbcluj.ro
网址:http://www.cs.ubbcluj.ro/~anca
Adresa:Campus 406,电话:
互惠生:Calcul evolutiv,Retele complexe,Automatic celulare
  

我使用了flutter插件Flutter_html来提取数据并将其插入到类中   代码如下:

 ListView.builder(
    itemCount: profIdentity == null ? 0 : profIdentity.length,
    itemBuilder: (BuildContext context, int index) {
      if (index == 0) {
      } else {
        profesoriList.add(prof);
        print(profesoriList.length);
        print(profesoriList[profesoriList.length-1].name);
        erase();
      }
      if (index == 110) {
        profesoriList.add(prof);
      }
      return Column(
        children: <Widget>[
          Card(
            child: Html(
              data: profIdentity[index],
              //Optional parameters:
              padding: EdgeInsets.all(8.0),
              linkStyle: const TextStyle(
                color: Colors.redAccent,
                decorationColor: Colors.redAccent,
                decoration: TextDecoration.underline,
              ),
              onLinkTap: (url) {
                print("Opening $url...");
              },
              onImageTap: (src) {
                print(src);
              },
              useRichText: false,
              //Must have useRichText set to false for this to work
              customRender: (node, children) {
                if (node is dom.Element) {
                  switch (node.localName) {
                    case "img":
                      {
                        prof.photo =
                            node.attributes['src'].toString(); //photo
                        prof.name =
                            node.attributes['alt'].toString(); //nume
                        if (prof.name == null || prof.name == "") {
                          prof.name =
                              node.attributes['title'].toString(); //nume
                        }
                        return Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              shape: BoxShape.circle,
                              border: Border.all(
                                color: Colors.transparent,
                              ),
                              boxShadow: [
                                BoxShadow(
                                    color: Colors.grey.withOpacity(0.3),
                                    offset: new Offset(0.0, 0.0),
                                    blurRadius: 2.0,
                                    spreadRadius: 0.0),
                              ]),
                          child: Padding(
                            padding: const EdgeInsets.all(4.0),
                            child: CircleAvatar(
                              radius: 40.0,
                              backgroundImage:
                                  NetworkImage(node.attributes['src']),
                            ),
                          ),
                        );
                      }
                    case "a":
                      {
                        prof.web = node.innerHtml; //pagina web
                      }
                  }
                } else if (node is dom.Text) {
                  //We don't need to worry about rendering extra whitespace
                  if (node.text.trim() == "" &&
                      node.text.indexOf(" ") == -1) {
                    return Wrap();
                  }
                  if (node.text.trim() == "" &&
                      node.text.indexOf(" ") != -1) {
                    node.text = " ";
                  }

                  String finalText = trimStringHtml(node.text);
                  if (finalText.endsWith(" ")) {
                    return Container(
                        padding: EdgeInsets.only(right: 2.0),
                        child: Text(finalText));
                  } else {
                    if (finalText.contains('E-mail')) {
                      prof.email = finalText.replaceFirst("[at]", "@");
                    } else if (finalText.contains('Web')) {
                      prof.web = finalText;
                    } else if (finalText.contains('Adresa')) {
                      prof.adress = finalText;
                    } else if (finalText.contains('Domenii')) {
                      prof.domainsOfInterest = finalText;
                    } else {}
                  }
                }
                return null;
              },
            ),
          ),
        ],
      );
    },
  ),

问题是我不知道为什么每次我在列表(profesoriList.add(prof);)中添加新教授时,都会同时更改其他所有教授。 enter image description here

0 个答案:

没有答案