def self.sang_carwler
agent = Mechanize.new
cc1 = ConsumableCategory.find_or_create_by(name: "生命科学", parent_id: 0)
resp = agent.get("https://www.XXX.com").search("li.subnav_cat dl.sub_dl")
resp.each do |e1|
catalog_name2 = e1.search(".sub_dt a").first.children.first.text.strip
next unless ["生化试剂", "分子生物学", "蛋白质科学", "抗体", "细胞生物学"].include?(catalog_name2)
cc2 = ConsumableCategory.find_or_create_by(name: catalog_name2, parent_id: cc1.id)
e1.search(".sub_dd a").each do |e2|
catalog_name3 = e2.children.first.text.strip
cc3 = ConsumableCategory.find_or_create_by(name: catalog_name3, parent_id: cc2.id)
end
end
end
--------------
# 基于接口的查询
def self.guoyao_crawler(titles = [])
menu_resp = HTTParty.get "https://XXX/reagent-front/indexApi/goodsClassList"
menu_resp["data"].each do |catalog_1|
next unless titles.include?(catalog_1["gcName"])
cc1 = ConsumableCategory.find_or_create_by(name: catalog_1["gcName"], parent_id: 0)
catalog_1["classList"].each do |catalog_2|
next if catalog_1["gcName"] == "化学试剂" && ["通用试剂", "高纯试剂", "色谱应用", "分析标准品"].include?(catalog_2["gcName"])
cc2 = ConsumableCategory.find_or_create_by(name: catalog_2["gcName"], parent_id: cc1.id)
catalog_2["classList"].each do |catalog_3|
cc3 = ConsumableCategory.find_or_create_by(name: catalog_3["gcName"], parent_id: cc2.id)
if catalog_1["gcName"] == "化学试剂"
page_size = 100
page_no = 1
flag = true
while flag do
chemical_resp = HTTParty.get "https://XXX/reagent-front/goodsApi/getGoodsList?pageSize=#{page_size}&pageNo=#{page_no}&searchType=gcIdSearch&keyword=#{catalog_3['gcId']}"
if chemical_resp["data"].first["pageCount"].to_i > 0
chemical_resp["data"].first["listApiGoods"].each do |goods|
MenuChemical.find_or_create_by(consumable_category_id: cc3.id, cas: goods["casIndexNo"])
end
page_no += 1
else
flag = false
end
end
end
end
end
end
end
--------------
def self.worm(sleep_second = 0.15)
agent = Mechanize.new
['nav-1', 'nav-2', 'nav-3', 'nav-4'].each do |nav|
agent.get("http://www.xxxx/zh_cn/").search("li.level0.#{nav} ul li ul li a").each do |link1|
puts "#{link1.attributes['href'].value} #{link1.children.children.text}"
fenlei = link1.children.children.text
product_list = agent.get(link1.attributes['href'].value)
total_count = product_list.search(".toolbar-number").last&.children&.text.to_i
total_page = total_count % 15 == 0 ? (total_count / 15) : ((total_count / 15) + 1)
(1..total_page).to_a.each do |page|
product_list = agent.get("#{link1.attributes['href'].value}?p=#{page}")
break if product_list.search('div.actions-primary a').blank?
product_list.search('div.actions-primary a').each do |product_link|
sleep sleep_second
product_page = agent.get(product_link.attributes['href'].value)
product_no = product_link.attributes['href'].value.gsub('http://www.xxx/zh_cn/','').gsub('.html', '').upcase
cas = product_page.search("#product_addtocart_form > div.product-shop > div.product-info > span:nth-child(2) > a").children.text
if product_page.search("#super-product-table thead tr").children.select{|c| c.name == 'th'}.map{|th| th.children.text.to_s.strip} == ["货号", "规格", "库存", "价格", "数量"]
product_page.search("#super-product-table tbody tr").each do |tr|
tds = tr.children.select{|c| c.name == 'td'}
package_unit = tds[0].children.text.to_s.strip.gsub("#{product_no}-",'')
package = package_unit.to_f
unit = package_unit.slice(/[a-zA-Z]+/).downcase
purity = tds[1].children.text.to_s.strip
stock = tds[2].children.text.to_s.strip
ajax_price_id = tds[3].attributes['attr'].value
price = 0
response = HTTParty.post("http://www.xxx/zh_cn/catalogb/ajax/price", body: {ajax_price_id => ajax_price_id})
price = Nokogiri::HTML(JSON.parse(response.parsed_response)[ajax_price_id]).search("p span.price").last.text.gsub(/[^0-9]/,'').to_f / 100 if response&.parsed_response.present?
ReagentCategory.create(name: 'ald', fenlei: fenlei, product_no: product_no, cas: cas, package: package, unit: unit, stock: stock, price: price, purity: purity, ajax_price_id: ajax_price_id, vendor_id: VENDOR_ID, company_id: COMPANY_ID)
end
end
end
end
end
end
end
分享到:
相关推荐
ruby mechanize 2.7.2 gem 包
ruby mechanize安装需要的gem包,安装需要的本地gem文件
Mechanize 一个让自动化web交互变得容易的ruby库
Ruby中实现网页抓取,一般用的是mechanize,使用非常简单。 安装 代码如下: sudo gem install mechanize 抓取网页 代码如下: require ‘rubygems’ require ‘mechanize’ agent = Mechanize.new page = agent.get...
ruby mechanize包,mechanize-2.7.3.gem
mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面。结合beautifulsoup和re模块,可以有效的解析web页面,我比较喜欢这种方法。 下面主要总结了使用mechanize模拟...
刮板:使用Nokogiri和Mechanize编译基于Ruby的手工刮板
机械化描述Mechanize库用于自动与网站进行交互。 Mechanize自动存储和发送Cookie,遵循重定向,并可以遵循链接和提交表单。 可以填写和提交表单字段。 机械化还可以将您曾经访问过的站点作为历史记录进行跟踪。依存...
Mechanize¶↑docs.seattlerb.org/mechanize github.com/sparklemotion/mechanize描述¶↑Mechanize库用于自动与网站交互。 Mechanize自动存储和...依赖项¶↑ruby 1.9.2或更高版本的nokogiri支持:¶↑Bug跟踪器可用
用python成为顶级黑客,第三方库必备,mechanize网络爬虫
本书共8章,涵盖的内容有Python语言的基本语法、Python常用IDE的使用、Python第三方模块的导入使用、Python爬虫常用模块、Scrapy爬虫、Beautiful Soup爬虫、Mechanize模拟浏览器和Selenium模拟浏览器。本书所有源...
本书共8章,涵盖的内容有Python语言的基本语法、Python常用IDE的使用、Python第三方模块的导入使用、Python爬虫常用模块、Scrapy爬虫、Beautiful Soup爬虫、Mechanize模拟浏览器和Selenium模拟浏览器。
您好如何等待页面加载完毕并在perl中获取页面内容
Mechanize相关资料
multi-mechanize-master
ruby版本:ruby1.8.7 or ruby1.9.2 操作系统:windows 7 Linux下没有试过~不过应该也可以的: 代码如下: #encoding: utf-8 require ‘rubygems’ require ‘mechanize’ class Renren def initialize(e,p) @...
6.3 使用Mechanize模块实现自动化表单处理 100 6.4 本章小结 102 第7章 验证码处理 103 7.1 注册账号 103 7.2 光学字符识别 106 7.3 处理复杂验证码 111 7.3.1 使用验证码处理服务 112 ...
本书共8章,涵盖的内容有Python语言的基本语法、Python常用IDE的使用、Python第三方模块的导入使用、Python爬虫常用模块、Scrapy爬虫、Beautiful Soup爬虫、Mechanize模拟浏览器和Selenium模拟浏览器。本书所有源...
本书共8章,涵盖的内容有Python语言的基本语法、Python常用IDE的使用、Python第三方模块的导入使用、Python爬虫常用模块、Scrapy爬虫、Beautiful Soup爬虫、Mechanize模拟浏览器和Selenium模拟浏览器。本书所有源...