英文:
How to collect data from website or (web scraping) via flutter?
问题
我需要从网站https://ipcost.com进行网页抓取并收集数据,然后在我的Flutter应用程序中使用这些收集的数据(在这种情况下是我的IP详细信息)。
我的问题是如何获取这些信息并在用户界面中显示它。
英文:
I need to web scraping and collect data from the website https://ipcost.com and use that collected data (in this case, my IP details) in my Flutter app.
My question is how to get this information and display it in the UI.
答案1
得分: 0
I tried the web_scraper package and it solved the problem.
Step 1 : 将 web_scraper 添加到依赖项中。
Step 2 : 检查网站并获取网站上所需信息的放置模式。在我的情况下,“Titles” 的模式是:
'div.e > small'
对于“Subtitle”,模式是:
'div.e > strong'
Step 3 : 我创建了一个 Webscraper 类的对象,并通过设置网站 URL 的值来定义它。
Step 4-1 : 在我的情况下,我不需要 urlPath,将其设置为空。
final String urlPath = '';
Step 4-2 : 在我的情况下,Attributes 总是为空,我不需要它们。
final List<String> attributeList = [''];
Step 5 : 我定义了一个方法,并在 initState 中调用它,以从网站获取值。
void fetchData() async {
if (await webScraper.loadWebPage(urlPath)) {
setState(() {
webScrapeResultTitle =
webScraper.getElement(innerAddressTitle, attributeList);
webScrapeResultDescription =
webScraper.getElement(innerAddressDescription, attributeList);
///TODO: 通过查看这些打印信息,我确定了接收到的数据结构。
// print("webScrapeResultTitle::::${webScrapeResultTitle!.toList()}");
// print("webScrapeResultDescription::::${webScrapeResultDescription!.toList()}");
});
}
}
Step 6 : 设置构建方法如下以显示结果。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: webScrapeResultTitle == null || webScrapeResultDescription == null
? Center(
child: LoadingWidget(),
)
: ListView.builder(
itemCount: webScrapeResultDescription!.length,
itemBuilder: (BuildContext context, int index) {
return IpInfoItem(
titleResult: webScrapeResultTitle![index][titleResultKey],
descriptionResult: webScrapeResultDescription![index]
[descriptionResultKey]);
}),
);
}
}
从这里查看我的完整代码
Gist全文段:Gist.Github.com
仓库:Github.com
英文:
I tried the web_scraper package and it solved the problem.
Step 1 : add web_scraper to dependencies .
Step 2 : Checking the website and getting the pattern of placing the desired information on the website. in my case
for "Titles" , pattern is :
'div.e > small'
for "Subtitle" , pattern is :
'div.e > strong'
Step 3 : I created an object of the Webscraper class and defined it by setting the value of the website URL.
Step 4-1 :
In my case , I don't need urlPath and set it empty .
final String urlPath = '';
Step 4-2 : In my case , Attributes always null and I don't need them.
final List<String> attributeList = [''];
Step 5 : I define one method and call them in initState , for get values from website .
void fetchData() async {
if (await webScraper.loadWebPage(urlPath)) {
setState(() {
webScrapeResultTitle =
webScraper.getElement(innerAddressTitle, attributeList);
webScrapeResultDescription =
webScraper.getElement(innerAddressDescription, attributeList);
///TODO: By review this prints , I detect structure of recievied data .
// print("webScrapeResultTitle::::${webScrapeResultTitle!.toList()}");
// print(
// "webScrapeResultDescription::::${webScrapeResultDescription!.toList()}");
});
}
}
Step 6 : set build method like below for show results .
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: webScrapeResultTitle == null || webScrapeResultDescription == null
? Center(
child: LoadingWidget(),
)
: ListView.builder(
itemCount: webScrapeResultDescription!.length,
itemBuilder: (BuildContext context, int index) {
return IpInfoItem(
titleResult: webScrapeResultTitle![index][titleResultKey],
descriptionResult: webScrapeResultDescription![index]
[descriptionResultKey]);
}),
);
}
}
See my full code from here
Gist full Snippets: Gist.Github.com
Repository : Github.com
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论