- くだすれPython(超初心者用) その47【Ruby禁止】
299 :250[sage]:2020/03/30(月) 23:17:49.32 ID:+W5CUrfY - >>286
Ruby で作ったけど、最初の部分は省くけど、( >>251 を参照) li.newsFeed_item の子孫のa は、29件で、li.newsFeed_item の直下のa は、25件。 Python のfind_all("a")[0] は、子孫も含めるのじゃないか? 出力は、こういう感じ title:富士フ、アビガン治験開始へ3/30(月) 22:52 url:https://news.yahoo.co.jp/pickup/6355698 title:大連市 北九州にマスク20万枚3/30(月) 22:40 url:https://news.yahoo.co.jp/pickup/6355699 doc = Nokogiri::HTML( driver.page_source ) elements = doc.css( "li.newsFeed_item" ) # li.newsFeed_item の子孫のa は、29件 results = elements.map { |elem| elem.at_css( "a" ) } puts results.length results.each { |elem| puts "title:#{ elem.text } url:#{ elem.attr( "href" ) }" } puts "------------------------------------" # li.newsFeed_item の直下のa は、25件 ary = elements.each.each_with_object( [ ] ) do |elem, ary| elem.children.each do |child| if child.node_name == "a" ary.push child break end end end puts ary.length ary.each { |elem| puts "title:#{ elem.text } url:#{ elem.attr( "href" ) }" }
|