最好的库来做网络抓取

gyu*_*isc 11 language-agnostic class-library web-crawler

我想从不同的网页获取数据,例如餐馆的地址或给定地点的不同活动的日期等等.我可以用什么来从一组给定的站点中提取这些数据的最佳库?

Mik*_*ike 10

HTML Agility Pack For .net程序员很棒.它可以转换XML文档中的网页,可以使用XPath进行查询.

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a@href")
{
HtmlAttribute att = link"href";
att.Value = FixLink(att);
}
doc.Save("file.htm");
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到它. http://www.codeplex.com/htmlagilitypack


小智 10

如果使用python,请好好看看Beautiful Soup(http://crummy.com/software/BeautifulSoup).

一个非常强大的库,使刮刮成为一件轻而易举的事.


Dre*_*son 5

我认为这里的一般答案是使用任何语言 + http 库 + html/xpath 解析器。我发现使用 ruby​​ + hpricot 提供了一个很好的干净的解决方案:

require 'rubygems'
require 'hpricot'
require 'open-uri'

sites = %w(http://www.google.com http://www.stackoverflow.com)

sites.each do |site|
  doc = Hpricot(open(site))

  # iterate over each div in the document (or use xpath to grab whatever you want)
  (doc/"div").each do |div|
    # do something with divs here
  end
end
Run Code Online (Sandbox Code Playgroud)

有关 Hpricot 的更多信息,请参阅http://code.whytheluckystiff.net/hpricot/