> 在这里,我们看到如何创建一个简单的自动化 Web 爬虫在 Raspberry Pi 上运行,以便您可以自动执行简单的任务。
通过在 Raspberry Pi 上运行的网络爬虫,您可以自动执行枯燥的日常任务,例如价格监控或市场调查。
最近,我对 IoT 和 Raspberry Pi 产生了兴趣,因为我是 .NET 开发人员,所以我开始在 Linux 上研究 .NET Core。原因很简单,一个 Linux 便宜,可以随处运行。我使用 .NET Core 建立了我的网站,其在运行在 Linode 的 Ubuntu,价格为 $5/月。 接下来,我开始探索在 Linux 发行版 Raspbian 上运行的 Raspberry Pi。我的第一个项目是在 C# 中,构建一个运行在 Raspberry Pi 上的网络爬虫,以便从亚马逊或百思买等热门网站获得最新的购物优惠,然后将数据发布到 WebApi,以提供我的网站 [http://www.fairnet.com/meal 。
安装了 ".NET Core 跨平台开发" 工作负载(workload)的 Visual Studio 2017。你可以下载免费的社区版。
启动 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 代码可以在其上运行。
所需用品:
一旦 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
现在,为网页抓取工具创建一个文件夹,并使用 FTP 传输工程文件。 然后,运行 dotnet webcrawler
:
dotnet webcrawler.dll
原文链接:https://dzone.com/articles/run-net-core-web-crawler-on-raspberry-pi
观光\评论区