Java Web挖掘还是抓取或爬行?我应该使用什么工具/库?
我想爬网和保存一些网页为HTML。比如说,爬进数百个流行网站,只需保存他们的FrontPage和“关于”页面 我研究了很多问题,但无论是网络爬网还是网络抓取问题都没有找到答案 我应该使用什么库或工具来构建解决方案?或者甚至有一些现有的工具可以处理这个问题吗?试试。它是python的web抓取库。Java Web挖掘还是抓取或爬行?我应该使用什么工具/库?,java,python,web-crawler,web-scraping,web-mining,Java,Python,Web Crawler,Web Scraping,Web Mining,我想爬网和保存一些网页为HTML。比如说,爬进数百个流行网站,只需保存他们的FrontPage和“关于”页面 我研究了很多问题,但无论是网络爬网还是网络抓取问题都没有找到答案 我应该使用什么库或工具来构建解决方案?或者甚至有一些现有的工具可以处理这个问题吗?试试。它是python的web抓取库。 如果需要一个简单的python脚本,请尝试使用python。使用python时,您可能会对和感兴趣 Mechanize某种程度上模拟浏览器(包括代理、伪造浏览器标识、页面重定向等选项),并允许轻松获取表
如果需要一个简单的python脚本,请尝试使用python。使用python时,您可能会对和感兴趣 Mechanize某种程度上模拟浏览器(包括代理、伪造浏览器标识、页面重定向等选项),并允许轻松获取表单、链接等。。。但是文档有点粗糙/稀疏 一些示例代码(来自mechanize网站)为您提供了一个想法:
import mechanize
br = mechanize.Browser()
br.open("http://www.example.com/")
# follow second link with element text matching regular expression
html_response = br.follow_link(text_regex=r"cheese\s*shop", nr=1)
print br.title()
print html_response
BeautifulSoup允许非常轻松地解析html内容(可以使用mechanize获取),并支持正则表达式
一些示例代码:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html_response)
rows = soup.findAll('tr')
for r in rows[2:]: #ignore first two rows
cols = r.findAll('td')
print cols[0].renderContents().strip() #print content of first column
因此,上面这10行几乎都是复制粘贴,可以打印网站上每个表行的第一列内容。有一点陡峭的学习曲线,但可以配置为只有主页和“看起来像”(使用正则表达式过滤器)的页面会被爬网
更多开源Java(web)爬虫:这里确实没有好的解决方案。你是对的,因为你怀疑Python可能是最好的开始方式,因为它难以置信地支持正则表达式 为了实现类似的功能,强大的SEO(搜索引擎优化)知识将有所帮助,因为有效地为搜索引擎优化网页会告诉您搜索引擎的行为。我会从这样一个网站开始 就识别“关于我们”页面而言,您只有两个选项: a) 对于每个页面,获取关于我们页面的链接并将其提供给爬虫 b) 分析页面的所有链接,查找某些关键字,如“关于我们”、“关于”“了解更多”或其他内容 在使用选项b时,请小心,因为您可能会陷入无限循环,因为网站会多次链接到同一页面,尤其是当链接位于页眉或页脚时,页面甚至可能会链接回自身。为了避免这种情况,您需要创建一个访问过的链接列表,并确保不要再次访问它们 最后,我建议您的爬虫程序遵守
robot.txt
文件中的说明,最好不要遵循标记为rel=“nofollow”
的链接,因为这些链接主要用于外部链接。再次,通过阅读SEO了解这一点和更多
关于,如果要构建爬虫程序,您需要(特定于Java):
断言1如何识别“About”页面?比方说,只需在url中为“About”匹配正则表达式即可。如果匹配,则保存它,如果不匹配,则保留它。假阴性没有问题。@Flake为了安全起见,文本中带有大约的URL是内部链接的。当然,有些人可能只是有像“公司信息”这样的链接,而没有“关于”这个词。嗨,乔尔,谢谢你有趣的回复。如果你能通过我的个人资料与我联系,进一步谈论网络爬虫,我很有兴趣与你进行更深入的交谈(如果你感兴趣,最终会聘用你)。谢谢:)
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vi:ts=4:et
# $Id: retriever-multi.py,v 1.29 2005/07/28 11:04:13 mfx Exp $
#
# Usage: python retriever-multi.py <file with URLs to fetch> [<# of
# concurrent connections>]
#
import sys
import pycurl
# We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
# the libcurl tutorial for more info.
try:
import signal
from signal import SIGPIPE, SIG_IGN
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
except ImportError:
pass
# Get args
num_conn = 10
try:
if sys.argv[1] == "-":
urls = sys.stdin.readlines()
else:
urls = open(sys.argv[1]).readlines()
if len(sys.argv) >= 3:
num_conn = int(sys.argv[2])
except:
print "Usage: %s <file with URLs to fetch> [<# of concurrent connections>]" % sys.argv[0]
raise SystemExit
# Make a queue with (url, filename) tuples
queue = []
for url in urls:
url = url.strip()
if not url or url[0] == "#":
continue
filename = "doc_%03d.dat" % (len(queue) + 1)
queue.append((url, filename))
# Check args
assert queue, "no URLs given"
num_urls = len(queue)
num_conn = min(num_conn, num_urls)
assert 1 <= num_conn <= 10000, "invalid number of concurrent connections"
print "PycURL %s (compiled against 0x%x)" % (pycurl.version, pycurl.COMPILE_LIBCURL_VERSION_NUM)
print "----- Getting", num_urls, "URLs using", num_conn, "connections -----"
# Pre-allocate a list of curl objects
m = pycurl.CurlMulti()
m.handles = []
for i in range(num_conn):
c = pycurl.Curl()
c.fp = None
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 30)
c.setopt(pycurl.TIMEOUT, 300)
c.setopt(pycurl.NOSIGNAL, 1)
m.handles.append(c)
# Main loop
freelist = m.handles[:]
num_processed = 0
while num_processed < num_urls:
# If there is an url to process and a free curl object, add to multi stack
while queue and freelist:
url, filename = queue.pop(0)
c = freelist.pop()
c.fp = open(filename, "wb")
c.setopt(pycurl.URL, url)
c.setopt(pycurl.WRITEDATA, c.fp)
m.add_handle(c)
# store some info
c.filename = filename
c.url = url
# Run the internal curl state machine for the multi stack
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Check for curl objects which have terminated, and add them to the freelist
while 1:
num_q, ok_list, err_list = m.info_read()
for c in ok_list:
c.fp.close()
c.fp = None
m.remove_handle(c)
print "Success:", c.filename, c.url, c.getinfo(pycurl.EFFECTIVE_URL)
freelist.append(c)
for c, errno, errmsg in err_list:
c.fp.close()
c.fp = None
m.remove_handle(c)
print "Failed: ", c.filename, c.url, errno, errmsg
freelist.append(c)
num_processed = num_processed + len(ok_list) + len(err_list)
if num_q == 0:
break
# Currently no more I/O is pending, could do something in the meantime
# (display a progress bar, etc.).
# We just call select() to sleep until some more data is available.
m.select(1.0)
# Cleanup
for c in m.handles:
if c.fp is not None:
c.fp.close()
c.fp = None
c.close()
m.close()