gpt4 book ai didi

RUBY - 网页抓取 - (OpenURI::HTTPError)

转载 作者:数据小太阳 更新时间:2023-10-29 07:12:12 27 4
gpt4 key购买 nike

我正在尝试用 ruby​​ 编写一个简单的网络抓取代码。它一直工作到第 29 个 url,然后我收到此错误消息:

C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:346:in `open_http': 500 Internal Server Er
ror (OpenURI::HTTPError)
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:775:in `buffer_open'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:677:in `open'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from test.rb:24:in `block (2 levels) in <main>'
from test.rb:18:in `each'
from test.rb:18:in `block in <main>'
from test.rb:14:in `each'
from test.rb:14:in `<main>'

我的代码:

require 'rubygems'  
require 'nokogiri'
require 'open-uri'

aFile=File.new('data.txt', 'w')

ag = 0
for i in 1..40 do
agenzie = ag + 1

#change url parameter

url = "http://www.infotrav.it/dettaglio.do?sort=*RICOVIAGGI*&codAgenzia=" + "#{ ag }"
doc = Nokogiri::HTML(open(url))
aFile=File.open('data.txt', 'a')
aFile.write(doc.at_css("table").text)
aFile.close
end

你有什么解决办法吗?谢谢!

aS

最佳答案

在这里,让我为您清理一下:

File.open('data.txt', 'w') do |aFile|
(1..40).each do |ag|
url = "http://www.infotrav.it/dettaglio.do?sort=*RICOVIAGGI*&codAgenzia=#{ag}"
response = open(url) rescue nil
next unless response
doc = Nokogiri::HTML(response)
aFile << doc.at_css("table").text
end
end

注意事项:

  • 使用 block 样式 File.open 意味着文件将在阻止导出
  • 使用 each 来迭代而不是 for 循环

关于RUBY - 网页抓取 - (OpenURI::HTTPError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12736201/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com