gpt4 book ai didi

ruby-on-rails - 循环内的 Rails Mechanize 循环

转载 作者:行者123 更新时间:2023-12-04 16:18:57 26 4
gpt4 key购买 nike

嗨,我设置了一个 Mechanize 任务来循环浏览一些列表并获取基本信息并保存到 csv

task :melrooftop => :environment do
require 'mechanize'
require 'csv'

mechanize = Mechanize.new
mechanize.history_added = Proc.new { sleep 10.0 }
mechanize.ignore_bad_chunking = true
mechanize.follow_meta_refresh = true
page = mechanize.get('https://www.theurbanlist.com/melbourne/directory/search/eyJyZXN1bHRfcGFnZSI6Im1lbGJvdXJuZVwvZGlyZWN0b3J5XC9zZWFyY2giLCJrZXl3b3JkcyI6IlJvb2Z0b3AiLCJjYXRlZ29yeTpidXNpbmVzc190eXBlIjoiMiJ9')
results = []
results << ['name', 'streetAddress', 'addressLocality', 'postalCode', 'addressRegion', 'addressCountry', 'telephone', 'url', 'tags']

page.search('a[title="view business"]').each do |link|
mechanize.click(link)

name = mechanize.page.css('article h1[itemprop="name"]').text.strip
streetAddress = mechanize.page.css('address span span[itemprop="streetAddress"]').text.strip
addressLocality = mechanize.page.css('address span span[itemprop="addressLocality"]').text.strip
postalCode = mechanize.page.css('address span span[itemprop="postalCode"]').text.strip
addressRegion = mechanize.page.css('address span span[itemprop="addressRegion"]').text.strip
addressCountry = mechanize.page.css('address span meta[itemprop="addressCountry"]').text.strip
telephone = mechanize.page.css('address span[itemprop="telephone"]').text.strip
url = mechanize.page.css('article p a[itemprop="url"]').text.strip
results << [name, streetAddress, addressLocality, postalCode, addressRegion, addressCountry, telephone, url, tags]
end

CSV.open("melrooftop.csv", "w+") do |csv_file|
results.each do |row|
csv_file << row
end
end

在这个循环中,我想收集一个标签列表并将它们添加到我的结果中。

我可以在我的循环中添加一个循环吗?
  tags = []
tags = mechanize.page.css('div[class="column medium-3 __left-col-small"] article ul li').each do |tag|
tag.text.strip
end

最佳答案

是的,或者您可以在一行上完成:

tags = page.search(css).map{|el| el.text}

关于ruby-on-rails - 循环内的 Rails Mechanize 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716334/

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