- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我需要从站点的所有页面收集所有“标题”。
站点具有 HTTP 基本身份验证配置。
没有授权我接下来做:
require 'anemone'
Anemone.crawl("http://example.com/") do |anemone|
anemone.on_every_page do |page|
puts page.doc.at('title').inner_html rescue nil
end
end
但是我对 HTTP Basic Auth 有一些问题...
如何使用 HTTP Basic Auth 从站点收集标题?
如果我尝试使用“Anemone.crawl(” http://username:password@example.com/ “)”,那么我只有第一页标题,但其他链接有 http://example.com/样式,我收到 401 错误。
最佳答案
HTTP 基本身份验证通过 HTTP header 工作。愿意访问受限资源的客户端必须提供身份验证 header ,如下所示:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
它包含名称和密码,Base64 编码。更多信息在维基百科文章中:Basic Access Authentication .
我用谷歌搜索了一下,没有找到让 Anemone 接受自定义请求 header 的方法。也许你会有更多的运气。
但我发现了另一个声称可以做到的爬虫:Messie .也许你应该试一试
这是 Anemone 设置其请求 header 的地方:Anemone::HTTP .事实上,那里没有定制。你可以猴子修补它。像这样的东西应该可以工作(把它放在你的应用程序的某个地方):
module Anemone
class HTTP
def get_response(url, referer = nil)
full_path = url.query.nil? ? url.path : "#{url.path}?#{url.query}"
opts = {}
opts['User-Agent'] = user_agent if user_agent
opts['Referer'] = referer.to_s if referer
opts['Cookie'] = @cookie_store.to_s unless @cookie_store.empty? || (!accept_cookies? && @opts[:cookies].nil?)
retries = 0
begin
start = Time.now()
# format request
req = Net::HTTP::Get.new(full_path, opts)
response = connection(url).request(req)
finish = Time.now()
# HTTP Basic authentication
req.basic_auth 'your username', 'your password' # <<== tweak here
response_time = ((finish - start) * 1000).round
@cookie_store.merge!(response['Set-Cookie']) if accept_cookies?
return response, response_time
rescue Timeout::Error, Net::HTTPBadResponse, EOFError => e
puts e.inspect if verbose?
refresh_connection(url)
retries += 1
retry unless retries > 3
end
end
end
end
显然,您应该为 basic_auth
方法调用提供您自己的 username
和 password
参数值。是的,它又快又脏又硬编码。但有时你没有时间以适当的方式做事。 :)
关于ruby - 使用 Anemone Web Spider 进行 HTTP 基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16846089/
使用 Anemone,我在尝试使用 MongoDB 时遇到此错误: gems/anemone-0.7.2/lib/anemone/storage.rb:28:in `MongoDB': uniniti
我们可以用 Anemone 抓取一个漏洞网站(例如:https://stackoverflow.com/),但是如果我只想关注某个文件夹怎么办(例如:https://stackoverflow.com
我不明白如何使用 Anemone 的触手部分。如果我对它的解释是正确的,我觉得我可以用它来从根目录中清除特定的页面深度。 Anemone.crawl(start_url) do |anemone|
代码: require 'anemone' Anemone.crawl("http://www.example.com/") do |anemone| anemone.on_every_page
我正在使用 Anemone 。我如何抓取子域?例如,如果我有网站 www.abc.com,我的抓取工具也应该抓取 support.abc.com 或 blah.abc.com。我正在使用 Ruby 1
我通过以下方式使用 Anemone gem: 访问第一个 url(种子),将页面内容保存到数据库,并将该页面的所有链接也保存到数据库(所有尚未在数据库中的链接) 从数据库中加载下一个链接,再次保存其内
我正在使用 Anemone爬取一个域,它工作正常。 启动抓取的代码如下所示: require 'anemone' Anemone.crawl("http://www.example.com/") do
我在 ruby 上运行一个带有 Anemone 的网络抓取工具,当我的服务器访问需要登录的页面时,我的服务器出现了一些问题。 所有页面的 url 中都有一个短语,例如“account”,我希望程序
我有一个抓取设置: require 'anemone' Anemone.crawl("http://www.website.co.uk", :depth_limit => 1) do |anemone
我希望将在线的数万页政府数据(在数千个文件夹中)拼凑起来,并将其全部放入一个文件中。为了加快这个过程,我想我会先把网站下载到我的硬盘上,然后再用像 Anemone + Nokogiri 这样的东西来抓
现在,我正在使用 Anemone 执行以下操作: Anemone.crawl("http://www.findbrowsenodes.com/", :delay => 3) do |anemone|
我正在努力进行爬网,但在我对整个网站进行爬网之前,我想对大约几个页面进行一次测试。所以我在想像下面这样的东西会起作用,但我一直收到 nomethoderror.... Anemone.crawl(se
我的起始页为 http://www.example.com/startpage它有 1220 个列表,按标准方式分页,例如每页 20 个结果。 我有代码可以解析结果的第一页,并跟踪在其 url 中包含
我正在创建某种网络爬虫,当我使用没有存储空间的 Anemone 来爬网网站时,它最终会因内存问题而崩溃。 所以我安装了 Redis、redis-rb 等,并更改了我的代码以使用 redis 存储。现在
我想将 URL on_pages_like 保存为特定匹配项。 Anemone 正在做它的事情,并且正在创建存储 URL 的记录,但是: 我想使用 find_or_create_by_url 而不是
我需要从站点的所有页面收集所有“标题”。 站点具有 HTTP 基本身份验证配置。 没有授权我接下来做: require 'anemone' Anemone.crawl("http://example.
在下面的示例中,我希望 anemone 仅在根 URL (example.com) 上执行。我不确定是否应该应用 on_page_like 方法,如果是的话我需要什么模式。 require 'an
我正在使用 anemone gem(Ruby- 1.8.7 和 Rails 3.1.1)开发爬虫。我应该如何从抓取/下载中跳过带有扩展名 pdf、doc、zip 等的网页。 最佳答案 ext = %w
我最近开始学习网络爬虫,我用 Ruby 构建了一个示例爬虫,Anemone , 和 Mongodb用于存储。我正在一个可能包含数十亿个链接的大型公共(public)网站上测试爬虫。 crawler.r
假设我正在尝试抓取一个网站并跳过这样结束的页面: http://HIDDENWEBSITE.com/anonimize/index.php?page=press_and_news&subpage=20
我是一名优秀的程序员,十分优秀!