我想解析受CloudFlare保护的站点。和此块中的最后一行:
agent = Mechanize.new
refs = agent.get(site).links_with(text: 'test')抛出异常,输出如下:
c:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/mechanize-2.7.3/lib/mechanize/http/agent.rb:308:in 'fetch': 503 => Net::HTTPServiceUnvailable for <site> - unhandled response (Mechanize::ResponseCodeError)
from c:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/mechanize-2.7.3/lib/mechanize.rb:440:in 'get'
from script.rb:10:in '<main>'如何修复它?
发布于 2015-05-12 05:26:36
您可能想要联系网站所有者,看看他们是否会将您的IP列入白名单。您所做的可能看起来像是一次攻击&一个安全特性正在被触发。
发布于 2015-05-11 02:24:33
发布于 2022-01-06 00:09:29
这是我的简单而实用的解决方案。在浏览器中打开受Cloudflare保护的网站,然后复制用户代理字符串和cf_clearance cookie的值。
require 'mechanize'
agent = Mechanize.new
agent.user_agent = '<YOUR_USER_AGENT_STRING>'
# Add a cookie to pass Cloudflare protection
agent.cookie_jar << HTTP::Cookie.new(
'cf_clearance', '<CF_CLEARANCE_VALUE>', domain: '.<DOMAIN>', path: '/'
)
page = agent.get('https://<PROTECTED_WEBSITE>')
puts 'YAY!'https://stackoverflow.com/questions/30151080
复制相似问题