Abot 网络爬虫将网页或图片存储到文件夹中
Abot web crawler store web pages or just images into folder
我正在使用 Google Abot 网络爬虫,想知道如何将单独的网页或图片存储到一个文件夹中。我检查了显示以下内容的论坛。但是我不能多次存储到同一个文件中,这是否意味着我每次都必须创建不同的文件名,或者是否有更简单的存储网页的方法。另外,如果我只想存储图像,我应该使用什么选项?我检查了其他 Abot Whosebug 帖子,发现以下已爬网页面内容已被评论。如何使用它们仅存储图像?
//crawledPage.RawContent //raw html
//crawledPage.HtmlDocument //lazy loaded html agility pack object (HtmlAgilityPack.HtmlDocument)
//crawledPage.CSDocument //lazy loaded cs query object (CsQuery.Cq)
void crawler_ProcessPageCrawlCompleted(object sender, PageCrawlCompletedArgs e)
{
CrawledPage crawledPage = e.CrawledPage;
if (crawledPage.WebException != null || crawledPage.HttpWebResponse.StatusCode != HttpStatusCode.OK)
Console.WriteLine("Crawl of page failed {0}", crawledPage.Uri.AbsoluteUri);
else
Console.WriteLine("Crawl of page succeeded {0}", crawledPage.Uri.AbsoluteUri);
if (!string.IsNullOrEmpty(crawledPage.Content.Text))
File.WriteAllText(SOMEFILEPATH, crawledPage.Content.Text); //or crawledPage.Content.Bytes
}
P.S。我使用 crawledPage.HtmlDocument.Save(@"C://TESTCRAWL/FILE"+rnd.Next(1, 100).ToString()+".html",[=21= 来存储网页].UTF8);
有没有办法只获取图像?
ABot 不会自动下载图片,它是用来抓取网址的,您需要编写代码来提取图片网址,然后遍历所有网址
第 1 步: 使用 HtmlAgilityPack 从网页源中提取图像 SRC
List<string> imgScrs = new List<string>();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(crawledPage.Content.Text);
var nodes = doc.DocumentNode.SelectNodes(@"//img[@src]"); s
foreach (var img in nodes)
{
HtmlAttribute att = img["src"];
imgScrs.Add(att.Value)
}
第 2 步: 遍历列表中的每个 src 并在 c: 驱动器中下载图像
int i = 0;
foreach (string src in imgScrs)
{
client.DownloadFile(new Uri(src), @"c:\temp\image_" + i +".jpg");
i++;
}
注意: :我正在使用 "i" 变量为每个图像指定一个唯一的名称,否则每次都会覆盖相同的图像
现在您可以让 Abot (c#) 为您下载图像。至少有 2 个解决方案。
准备
在每个解决方案中创建并使用您的自定义 CrawlConfiguration
实例并将其传递给 SiteCrawler
构造函数。
在您的配置对象中包含您的图像类型 MIME,例如
config.DownloadableContentTypes = "text/html,application/json,text/plain,image/jpeg,image/pjpeg,*/*"
解决方案 1
- 创建您自己的
LinkSelector
继承自 HapHyperLinkParser
并将其传递给 SiteCrawler
构造函数。
- 在
LinkSelector
覆盖 GetHrefValues
。从下载的页面中提取图像 URLs 并将它们包含在返回的列表中。
- 通过引用
crawledPage.Content.Bytes
在您的 crawler_ProcessPageCrawlCompleted
处理程序中保存图像。
解决方案 2
在您的 crawler_ProcessPageCrawlCompleted
处理程序中提取图像 URLs 并像这样将它们添加到您的抓取程序调度程序中
e.CrawlContext.Scheduler.Add(new PageToCrawl(new Uri(pictureUrl)));
您的图片将以与任何其他 HTML 页面相同的方式下载。
通过引用 crawledPage.Content.Bytes
在您的 crawler_ProcessPageCrawlCompleted
处理程序中保存图像。
在任何一种情况下,您都可以通过例如第 URL.
页
好处
使用您的爬虫而不是单独的下载器有很多好处。
如果网站需要登录才能下载任何内容,您可以为爬虫建立会话而不用担心打开另一个会话。一些网站也阻止同一用户多次登录。
此外,您需要小心使用单独的下载程序,并确保它们不会为每个图像建立新的连接。我建议创建连接池并重用它。否则你可以关闭服务器。
我的偏好仍然是只使用爬虫。
我正在使用 Google Abot 网络爬虫,想知道如何将单独的网页或图片存储到一个文件夹中。我检查了显示以下内容的论坛。但是我不能多次存储到同一个文件中,这是否意味着我每次都必须创建不同的文件名,或者是否有更简单的存储网页的方法。另外,如果我只想存储图像,我应该使用什么选项?我检查了其他 Abot Whosebug 帖子,发现以下已爬网页面内容已被评论。如何使用它们仅存储图像?
//crawledPage.RawContent //raw html
//crawledPage.HtmlDocument //lazy loaded html agility pack object (HtmlAgilityPack.HtmlDocument)
//crawledPage.CSDocument //lazy loaded cs query object (CsQuery.Cq)
void crawler_ProcessPageCrawlCompleted(object sender, PageCrawlCompletedArgs e)
{
CrawledPage crawledPage = e.CrawledPage;
if (crawledPage.WebException != null || crawledPage.HttpWebResponse.StatusCode != HttpStatusCode.OK)
Console.WriteLine("Crawl of page failed {0}", crawledPage.Uri.AbsoluteUri);
else
Console.WriteLine("Crawl of page succeeded {0}", crawledPage.Uri.AbsoluteUri);
if (!string.IsNullOrEmpty(crawledPage.Content.Text))
File.WriteAllText(SOMEFILEPATH, crawledPage.Content.Text); //or crawledPage.Content.Bytes
}
P.S。我使用 crawledPage.HtmlDocument.Save(@"C://TESTCRAWL/FILE"+rnd.Next(1, 100).ToString()+".html",[=21= 来存储网页].UTF8); 有没有办法只获取图像?
ABot 不会自动下载图片,它是用来抓取网址的,您需要编写代码来提取图片网址,然后遍历所有网址
第 1 步: 使用 HtmlAgilityPack 从网页源中提取图像 SRC
List<string> imgScrs = new List<string>();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(crawledPage.Content.Text);
var nodes = doc.DocumentNode.SelectNodes(@"//img[@src]"); s
foreach (var img in nodes)
{
HtmlAttribute att = img["src"];
imgScrs.Add(att.Value)
}
第 2 步: 遍历列表中的每个 src 并在 c: 驱动器中下载图像
int i = 0;
foreach (string src in imgScrs)
{
client.DownloadFile(new Uri(src), @"c:\temp\image_" + i +".jpg");
i++;
}
注意: :我正在使用 "i" 变量为每个图像指定一个唯一的名称,否则每次都会覆盖相同的图像
现在您可以让 Abot (c#) 为您下载图像。至少有 2 个解决方案。
准备
在每个解决方案中创建并使用您的自定义 CrawlConfiguration
实例并将其传递给 SiteCrawler
构造函数。
在您的配置对象中包含您的图像类型 MIME,例如
config.DownloadableContentTypes = "text/html,application/json,text/plain,image/jpeg,image/pjpeg,*/*"
解决方案 1
- 创建您自己的
LinkSelector
继承自HapHyperLinkParser
并将其传递给SiteCrawler
构造函数。 - 在
LinkSelector
覆盖GetHrefValues
。从下载的页面中提取图像 URLs 并将它们包含在返回的列表中。 - 通过引用
crawledPage.Content.Bytes
在您的crawler_ProcessPageCrawlCompleted
处理程序中保存图像。
解决方案 2
在您的
crawler_ProcessPageCrawlCompleted
处理程序中提取图像 URLs 并像这样将它们添加到您的抓取程序调度程序中e.CrawlContext.Scheduler.Add(new PageToCrawl(new Uri(pictureUrl)));
您的图片将以与任何其他 HTML 页面相同的方式下载。
通过引用
crawledPage.Content.Bytes
在您的crawler_ProcessPageCrawlCompleted
处理程序中保存图像。
在任何一种情况下,您都可以通过例如第 URL.
页好处
使用您的爬虫而不是单独的下载器有很多好处。
如果网站需要登录才能下载任何内容,您可以为爬虫建立会话而不用担心打开另一个会话。一些网站也阻止同一用户多次登录。
此外,您需要小心使用单独的下载程序,并确保它们不会为每个图像建立新的连接。我建议创建连接池并重用它。否则你可以关闭服务器。
我的偏好仍然是只使用爬虫。