Flutter:如何解析HTML属性中的"title"数据并显示在ListView中?
Flutter: How can I parse the "title" data in the HTML attribute and display it in ListView?
我在解析和过滤我需要的 HTML 数据时遇到问题。我通过 CSS 选择器接近数据,但是当我在 ListView 中打印过滤后的数据时,它在 ListView 中只显示“”。我使用 Dio 获取 HTML 源,并使用 html API(不是 dart:html)解析和过滤数据。如何使用Flutter解析HTML属性中的“标题”数据并显示在列表视图中?非常感谢你的帮助。谢谢。
这是我解析数据的Flutter代码,有注释的代码是我试过的代码
setState(() {
document = parse(response.data);
//elements = document.getElementsByClassName('samu');
//elements = document.getElementsByTagName('a');
elements = document.querySelectorAll("td.subject > a.samu");
//elements = elements.toString();
});
}
这是我创建 Listview 的代码
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('크롤링 리스트'),
),
body:
//Text(_text));
ListView.builder(
itemCount: elements.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${elements[index]}'),
);
},
));
}
这是我要解析的 HTML 代码。我想过滤(或获取)“标题”部分的数据。在我使用的 HTML 源中,有很多使用“title”的无用数据,但我需要的所有数据都在 class “samu”中关联。
<td class="subject"> <a href="javascript:void(0);" class="samu" onclick="fnView('BBSMSTR_000000012758', '19151090'); " title="title 1">
title 1</a>
您可以在解析 html 之前提取 title
。
假设response.data
returns一个列表<td>....</td>
html。您必须迭代每个 <td>...</td>
并提取标题。
例如,
假设您得到如下列表:
"data": [
"<td>...</td>",
"<td>...</td>",
...
"<td>...</td>",
]
只需迭代每个元素并使用下面的 getTitle
方法提取标题。
String getTitle(String html) {
// html = '''<td class="subject"> <a href="javascript:void(0);" class="samu" onclick="fnView('BBSMSTR_000000012758', '19151090'); " title="title 1">
// title 1</a>'''
if(html.contains('title=')) {
html = html.substring(html.indexOf('title='));
html = html.substring(6, html.indexOf('>'));
html = html.replaceAll('"', '');
print(html); // Prints title 1
}
return html;
}
我在解析和过滤我需要的 HTML 数据时遇到问题。我通过 CSS 选择器接近数据,但是当我在 ListView 中打印过滤后的数据时,它在 ListView 中只显示“”。我使用 Dio 获取 HTML 源,并使用 html API(不是 dart:html)解析和过滤数据。如何使用Flutter解析HTML属性中的“标题”数据并显示在列表视图中?非常感谢你的帮助。谢谢。
这是我解析数据的Flutter代码,有注释的代码是我试过的代码
setState(() {
document = parse(response.data);
//elements = document.getElementsByClassName('samu');
//elements = document.getElementsByTagName('a');
elements = document.querySelectorAll("td.subject > a.samu");
//elements = elements.toString();
});
}
这是我创建 Listview 的代码
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('크롤링 리스트'),
),
body:
//Text(_text));
ListView.builder(
itemCount: elements.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${elements[index]}'),
);
},
));
}
这是我要解析的 HTML 代码。我想过滤(或获取)“标题”部分的数据。在我使用的 HTML 源中,有很多使用“title”的无用数据,但我需要的所有数据都在 class “samu”中关联。
<td class="subject"> <a href="javascript:void(0);" class="samu" onclick="fnView('BBSMSTR_000000012758', '19151090'); " title="title 1">
title 1</a>
您可以在解析 html 之前提取 title
。
假设response.data
returns一个列表<td>....</td>
html。您必须迭代每个 <td>...</td>
并提取标题。
例如, 假设您得到如下列表:
"data": [
"<td>...</td>",
"<td>...</td>",
...
"<td>...</td>",
]
只需迭代每个元素并使用下面的 getTitle
方法提取标题。
String getTitle(String html) {
// html = '''<td class="subject"> <a href="javascript:void(0);" class="samu" onclick="fnView('BBSMSTR_000000012758', '19151090'); " title="title 1">
// title 1</a>'''
if(html.contains('title=')) {
html = html.substring(html.indexOf('title='));
html = html.substring(6, html.indexOf('>'));
html = html.replaceAll('"', '');
print(html); // Prints title 1
}
return html;
}