【问题标题】:Flutter - Error parsing HTML dataFlutter - 解析 HTML 数据时出错
【发布时间】:2018-08-17 16:11:25
【问题描述】:

我一直在尝试修改代码available here,以便VMoneyNews 中的titledateAdded 在应用程序listView 中可见。

不幸的是,当我尝试运行以下编辑后的代码时,我得到了下图中所述的错误。

http.Response response = await http.get('https://www.virtualmoneysnews.com/category/bitcoin-news/');

//parse and extract the data from the web site
dom.Document document = parser.parse(response.body);
document.getElementsByTagName('article').forEach((child) {
  jobsList.add(Job(
    title: child.getElementsByClassName('title').first.text,
    dateAdded: child.getElementsByClassName('thetime date updated').last.text,

  ));
});

【问题讨论】:

  • 我需要在这里做什么?
  • 你能用完整的sn-p更新cde吗,上面没有trim方法。
  • 我从未添加过修剪方法,上面是我更改的唯一代码,您认为这是问题所在吗?

标签: android html ios dart flutter


【解决方案1】:

您可能应该删除此 sn-p,它依赖于您未填充的 item.location

            Text(
              item.location.trim(),
              style: TextStyle(
                  color: Colors.white, fontWeight: FontWeight.bold),
            ),

我会重构 Job 类,将其重命名为 ArticleHeadlineStory,即有意义的东西。例如:

class Story {
  String title;
  String dateAdded;

  Story(this.title, this.dateAdded);

  @override
  String toString() => '$title $dateAdded';
}

然后可以编写您的抓取代码:

http.Response response = await http
    .get('https://www.virtualmoneysnews.com/category/bitcoin-news/');
dom.Document document = parser.parse(response.body);

List<Story> stories = document
    .getElementsByTagName('article')
    .map((e) => Story(
          e.getElementsByClassName('title').first.text,
          e.getElementsByClassName('thetime date updated').last.text,
        ))
    .toList();

【讨论】:

  • 这正是我要找的,感谢理查德的帮助
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 2017-09-12
  • 1970-01-01
相关资源
最近更新 更多