Rubinmechanisierungstischabbildung erfasst nicht ganze Zeile
-
14-11-2019 - |
Frage
Ich versuche, eine Tischwebsite mit Mechanisierung zu kratzen. Ich möchte die zweite Reihe kratzen.
wenn ich renne:
agent.page.search('table.ea').search('tr')[-2].search('td').map{ |n| n.text }
Ich würde erwarten, dass es die ganze Reihe kratzen würde.Stattdessen kratzt es nur: ["2011-02-17", "0,00"]
Warum kratzt es nicht alle Säulen in der Zeile, sondern nur die erste und die letzte Spalte?
xpath: / html / körper / zentrum / tabelle / tborne / tr [2] / td [2] / tabelle / tbody / tr [3] / td / table / tborde / tr [2] / td / tabelle / tborne / tr [2]
css path: HTML Body Center Table Tbody TT TD-Tabellen-TDE-TD-Tabelle T-TD-TD-Tabelle
Die Seite ist ähnlich: generasacodicetagpre.
Lösung
Using the following Ruby code (https://gist.github.com/835603):
require 'mechanize'
require 'pp'
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
a.get('http://binarymuse.net/table.html') do |page|
pp page.search('table.ea').search('tr')[-2].search('td').map{ |n| n.text }
end
I get the following output:
["2011-02-17", "0", "0", "0,00", "0", "0", "0", "0", "387", "0,00", "0,00"]
Andere Tipps
I would recommend you to leave Mechanize to harder stuff than scraping a page. You can use Nokogiri much more simple than using Mechanize(but ofcourse you can do it with it) since you can just query the page.
Try it out!
here is a link to an answer regarding nokogiri
Personally I used Mechanize when I needed to send forms and stuff like that albeit there are tons of other uses to it!