How do I integrate these two conditions block codes to mine in Ruby?
How do I integrate these two conditions if my code scrapes without them? My code is working already, but it scrapes all rows (non-bold and bold values) and doesn't scrape the title attribute string.
Condition 1: parses a table row only if one of its fields is bold:
doc = Nokogiri::HTML(html)
doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]').each do |row|
puts row.at_xpath('td[3]/text()')
end
Condition2: gets only the number off the title attribute string :
doc = Nokogiri::HTML(html)
numbers = doc.xpath('//p[@title]').collect { |p| p[:title].gsub(/[^\d]/, '') }
My code:
doc = Nokogiri::HTML(search_result.body)
rows = doc.css("table.articulos tr")
i = 0
details = rows.each do |row|
detail = {}
[
[:sku, 'td[3]/text()'],
[:desc, 'td[4]/text()'],
[:qty, 'td[5]/text()'],
[:qty2, 'td[5]/p/b/text开发者_如何转开发()'],
[:price, 'td[6]/text()']
].each do |name, xpath|
detail[name] = row.at_xpath(xpath).to_s.strip
end
i = i + 1
detail
end
A second attempt:
doc = Nokogiri::HTML(search_result.body)
rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]')
i = 0
details = rows.each do |row|
detail = {}
[
[:sku, 'td[3]/text()'],
[:desc, 'td[4]/text()'],
[:stock, "td[5]/p[@title]"],
[:price, 'td[6]/text()']
].each do |name, xpath|
detail[name] = row.at_xpath(xpath).to_s.strip
end
i = i + 1
if detail[:sku] != ""
price = detail[:price].split
if price[1] == "D"
currency = 144
else
currency = 168
end
stock = detail[:stock].gsub(/[^\d]/, '-')
cost = price[0].gsub(",", "").to_f
end
stock instead of just scraping the title string it scrapes the whole paragraph
<p-style="margin-top:-0px;-margin-bottom:0px;-cursor:hand"-title="2-en-su-sucursal"><b>10</b></p>
when I only want 2 from the title attribute
Here is my untested attempt to fix this:
doc = Nokogiri::HTML(search_result.body)
rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]')
i = 0
details = rows.each do |row|
detail = {}
[
[:sku, 'td[3]/text()'],
[:desc, 'td[4]/text()'],
[:stock, 'td[5]/p[@title]'],
[:price, 'td[6]/text()']
].each do |name, xpath|
if name == :stock
detail[name] = row.at_xpath(xpath).collect { |p| p[:title].gsub(/[^\d]/, '') }
else
detail[name] = row.at_xpath(xpath).to_s.strip
end
end
i = i + 1
detail
end
Here is my working code. maybe needs a little bit of cleaning but it works. the results are correct but I get a lot of nils.
doc = Nokogiri::HTML(search_result.body)
rows = doc.xpath('//table[@class="articulos"]/tr[td[5]/p/b]')
i = 0
details = rows.each do |row|
detail = {}
[
[:sku, 'td[3]/text()'],
[:desc, 'td[4]/text()'],
[:stock, "td[5]/p/@title"],
[:price, 'td[6]/text()']
].each do |name, xpath|
detail[name] = row.at_xpath(xpath).to_s.strip
end
i = i + 1
if detail[:sku] != ""
price = detail[:price].split
if price[1] == "D"
currency = 144
else
currency = 168
end
stock = detail[:stock].each do |anchor|
puts anchor['title']
end
stock1 = stock.gsub(/[^\d]/, '')
cost = price[0].gsub(",", "").to_f
end
精彩评论