当前位置:首页 > Web开发 > 正文

}# 发起请求res = HTTParty.get(page_url

2024-03-31 Web开发

标签:

环境配置:

安置httparty库

gem install httparty 代码样例

使用代办代理

require "httparty" # 引入httparty模块 require 'zlib' require 'stringio' # 代办代理处事器ip和端口 proxy_ip = '47.115.5.19' proxy_port = 16816 # 要访谒的方针网页, 以京东首页为例 page_url = 'https://dev.kuaidaili.com/testproxy' # 设置headers headers = { "User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50", "Accept-Encoding" => "gzip", } # 设置代办代理 options = { :headers => headers, :http_proxyaddr => proxy_ip, :http_proxyport => proxy_port, } # 倡议请求 res = HTTParty.get(page_url, options) # 输出状态码 puts "status code: #{res.code}" # 输出响应体 if res.code.to_i != 200 then puts "page content: #{res.body}" else gz = Zlib::GzipReader.new(StringIO.new(res.body.to_s)) puts "page content: #{gz.read}" end

运行下,,看当作果。

进阶学习

Ruby——httparty使用代办代理

标签:

原文地点:https://www.cnblogs.com/kuaidaili/p/12305452.html

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30159.html