在 Raspberry Pi 上运行 .NET Core Web Crawler






4.85/5 (9投票s)
使用在 Raspberry Pi 上运行的 Web Crawler,你可以自动化一项繁琐的日常任务,例如价格监控或市场调研。
引言
最近,我对 IOT 和 Raspberry Pi 产生了兴趣。由于我是一名 .NET 开发者,我开始探索 Linux 栈上的 .NET Core。原因很简单——因为 Linux 栈价格低廉且可以在任何地方运行,我使用 .NET Core 构建了一个运行在 Linode 上 Ubuntu 上的网站,每月只需 5 美元。接下来,我开始探索运行 Linux 发行版 Raspbian 的 Raspberry Pi。我的第一个项目是在 Raspberry Pi 上构建一个 C# Web Crawler,从 Amazon 或 Bestbuy 等热门网站获取最新的购物优惠,然后将数据发布到 WebApi,为我的网站提供数据,网址为 http://www.fairnet.com/deal。
必备组件
Visual Studio 2017,并安装了“.NET Core 跨平台开发”工作负载。你可以下载社区版,它是免费的。
Using the Code
启动 Visual Studio 2017。从菜单栏中选择 文件 > 新建 > 项目。在 新建项目* 对话框中,选择 Visual C# 节点,然后选择 .NET Core 节点。然后选择 控制台应用 (.NET Core) 项目模板。
安装 HtmlAgilityPack
和 Newtonsoft.Json NuGet 包。
HtmlAgilityPack
是一个敏捷的 HTML 解析器,它构建了一个可读/写的 DOM,并支持纯 XPATH 或 XSLT。
这是向网站发送请求以获取所有 HTML 页面的代码
HttpClient client = new HttpClient();
using (var response = await client.GetAsync(url))
{
using (var content = response.Content)
{
var result = await content.ReadAsStringAsync();
var document = new HtmlDocument();
document.LoadHtml(result);
var nodes = document.DocumentNode.SelectNodes
("//div[@class='item-inner clearfix']");
var storeData = new List<store>();
foreach (var node in nodes)
{
Store _store = ParseHtml(node);
storeData.Add(_store);
}
HttpResponseMessage resp = await client.PostAsJsonAsync<list<store>>
(@"/api/stores", storeData);
}
}
我将解析后的数据发布到 webApi,在那里它被保存到 MongoDB 中。
HttpResponseMessage resp = await client.PostAsJsonAsync >(@"/api/stores", storeData);
这是 ParseHtml
方法,用于解析有用的数据。
private static Store ParseHtml(HtmlNode node)
{
var _store = new Store();
_store.Image = node.Descendants("img").ElementAt(imgIndex).OuterHtml;
_store.Link = node.Descendants("a").Select
(s => s.GetAttributeValue
("href", "not found")).FirstOrDefault();
_store.Title = node.Descendants("a").ElementAt(titIndex).InnerText;
_store.Price = node.Descendants("span").ElementAt(pricIndex).InnerText;
_store.RetailPrice = node.Descendants("span").
ElementAt(retpricIndex).InnerText;
return _store;
}
接下来,我需要设置 Raspberry Pi,以便 .NET 代码可以在其上运行。
所需用品
- Raspberry Pi 3 Model B
- HDMI线
- USB 鼠标/键盘
- SD 卡
- 2 安培 USB 电源适配器
设置 Raspberry Pi
- 推荐的操作系统称为 Raspbian。从 https://www.raspberrypi.org/downloads/raspbian/ 下载它
- 将 .NET Core 2 安装到 Raspberry Pi 上
- 将此应用程序部署到运行 Raspbian 的 Pi 上
安装 Raspbian 后,配置 Raspberry Pi 以从开发机器连接。
从 Raspberry Pi 配置屏幕启用 SSH。
接下来,我们需要找到 Raspberry Pi 的 IP 地址。
在你的 Pi 上打开终端并输入
hostname -I
接下来,安装 PUTTY 以从你的开发机器连接。
Raspbian 的默认用户名和密码是“pi
”和“raspberry
”。
将 .NET Core 2 安装到 Raspberry Pi 上。
# Update the Raspbian install
sudo apt-get -y update
# Install the packages necessary for .NET Core
sudo apt-get -y install libunwind8 gettext
# Download the nightly binaries for .NET Core 2
wget https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/
dotnet-runtime-latest-linux-arm.tar.gz
# Create a folder to hold the .NET Core 2 installation
sudo mkdir /opt/dotnet
# Unzip the dotnet zip into the dotnet installation folder
sudo tar -xvf dotnet-runtime-latest-linux-arm.tar.gz -C /opt/dotnet
# set up a symbolic link to a directory on the path so we can call dotnet
sudo ln -s /opt/dotnet/dotnet /usr/local/bin
运行 dotnet --info
命令以查看安装在 Raspbian 上的版本。
为 linux-arm
创建 .NET 部署发布版本
dotnet publish -c release -r linux-arm
现在,创建一个 webcrawler 文件夹,并使用 FTP 传输项目文件。然后,运行 dotnet webcrawler
。
dotnet webcrawler.dll
关注点
我将在未来继续撰写关于开发此平台上的 IoT 应用程序的博客。
历史
- 2017 年 12 月 16 日:初始版本