ruby-on-rails - RoR 中的 Kimurai - 在解析中获取 current_user
问题描述
我正在尝试使用设计的导轨的kimurai 框架
应用程序/模型/products_spider.rb
class ProductsSpider < Kimurai::Base
@name = 'products_spider'
@engine = :mechanize
def self.process(url, user)
@start_urls = [url]
self.crawl!
end
def parse(response, url:, data: {})
Rails.logger.info "*** Spider - Start parsing"
Rails.logger.info " url: #{url}"
response.xpath("//div[@class='product floatleft span6 vertical-separator' or @class='product floatleft span6 ']").each do |product|
item = {}
item[:title] = product.css('h2.h-pr-title')&.text&.squish
item[:descr] = product.css('p.product_s_desc')&.text&.squish
item[:url] = URI.parse(url).merge(URI.parse(product.css('h2.h-pr-title a')[0]["href"])).to_s
item[:img_url] = URI.parse(url).merge(URI.parse(product.css('img')[0]["src"])).to_s
item[:price_cents] = product.css('span.PricesalesPrice')&.text.gsub(',', '.').to_f * 100
item[:published] = true
item[:categories] = response.xpath("//ul[@class='breadcrumb']/li[@class='active']/span").text.squish
add_event(:custom, :rec_total)
Product.where(item).first_or_create
if Product.where(item).exists?
add_event(:custom, :rec_exists)
else
Product.create!(item)
add_event(:custom, :rec_new)
end
end
end
end
在解析里面我想:
- 获取 current_user(设计会话),或
- 不知何故,我可以传递用户信息来解析,或者
- 我可以使用
parse!
which 可以返回items
数组,但在这种情况下,我如何知道该过程是否正常完成或有错误?使用crawl!
它时会返回response
包含此信息的...
应用程序/控制器/products_controller.rb
class ProductsController < ApplicationController
def scrape
url = 'https://manitaropolio.gr/proionta-manitaria/apoksiramena-manitaria'
flash.now[:notice] = "Scrape Url: #{url}"
response = ProductsSpider.process(url, current_user)
if response[:status] == :completed && response[:error].nil?
flash.now[:notice] = "Completed succesfully (added: #{response[:events][:custom][:rec_new]}, existed: #{response[:events][:custom][:rec_exists]}, total: #{response[:events][:custom][:rec_total]})"
else
flash.now[:alert] = response[:error]
end
rescue StandardError => e
flash.now[:alert] = "Error: #{e}"
end
配置/路由.rb
Rails.application.routes.draw do
devise_for :users
resources :products do
match '/scrape', to: 'products#scrape', via: :post, on: :collection
end
root to: "public#home"
end
应用程序/视图/产品/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Products</h1>
<% if can? :scrape, Product %>
<div class="row">
<div class="col">
<%= button_to 'Scrape', scrape_products_path %>
</div>
<% if @products.size > 0 %>
<div class="col text-right">
<%= "Count: #{@products.count}" %>
<%= " (Total Count: #{Product.count})" if current_user.role?(:admin) %>
</div>
<% end %>
</div>
<% end %>
关于如何做到这一点的任何想法?
谢谢
解决方案
推荐阅读
- python - 在 Python 中使用 ElementTree 从 xml 中获取数据
- bluetooth-lowenergy - 在 xcode 应用程序中实现蓝牙 LE 的问题
- reactjs - React-native-calendars(wix)议程视图对齐已损坏
- javascript - 动态填充 Jqgrid 下拉过滤器
- ios - Swift 获取 HTTP JSON 响应并使用 Codable 解析它
- rgb - 具有深度信息的 RGB 图像
- google-app-engine - 504 bad gateway nginx 应用引擎标准 python37 运行时 basic_scaling
- elasticsearch - elasticsearch:以“HH:mm:ss”格式添加持续时间
- react-native - React Native 子组件更新
- javascript - 在scrapy中获取document.write的输出