我希望能够在1001Tracklist上从跟踪列表页面中抓取数据。URL示例是:
http://www.1001tracklists.com/tracklist/25122_lange-intercity-podcast-115-2013-03-06.html下面是一个如何在页面上显示数据的示例:
Above & Beyond - Black Room Boy (Above & Beyond Club Mix) [ANJUNABEATS]我想以以下格式从这个页面中取出所有的歌曲:
$byArtist - $name [$publisher]在查看了此页面的HTML之后,显示我所追求的内容以HTML5元数据格式存储:
<td class="" id="tlptr_433662">
<a name="tlp_433662"></a>
<div itemprop="tracks" itemscope itemtype="http://schema.org/MusicRecording" id="tlp5_content">
<meta itemprop="byArtist" content="Above & Beyond">
<meta itemprop="name" content="Black Room Boy (Above & Beyond Club Mix)">
<meta itemprop="publisher" content="ANJUNABEATS">
<meta itemprop="url" content="/track/103905_above-beyond-black-room-boy-above-beyond-club-mix/index.html">
<span class="tracklistTrack floatL"id="tr_103905" ><a href="/track/103905_above-beyond-black-room-boy-above-beyond-club-mix/index.html" class="">Above & Beyond - Black Room Boy (Above & Beyond Club Mix)</a> </span><span class="floatL">[<a href="/label/1037_anjunabeats/index.html" title="Anjunabeats">ANJUNABEATS</a>]</span>
<div id="tlp5_actions" class="floatL" style="margin-top:1px;">有一个具有"tlp_433662“值的CSS选择器。页面上的每首歌都会有自己独特的标识。一个是"tlp_433662“,另一个是"tlp_433628”或类似的东西。
是否有一种方法可以使用Nokogiri和XPath提取跟踪列表页面中列出的所有歌曲?我可能希望在下面列出的“数据”上“做”一个“每个”,这样刮刀就会循环在数据上,提取每一组相关数据。下面是我的Ruby程序的开始:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.1001tracklists.com/tracklist/25122_lange-intercity-podcast-115-2013-03-06.html"
data = Nokogiri::HTML(open(url))
# what do do next? print out xpath loop code which extracts my data.
# code block I need help with
data.xpath.........each do |block|
block.xpath("...........").each do |span|
puts stuff printing out what I want.
end
end我的最终目标,我知道如何做,是把这个Ruby脚本到辛纳屈"webify“数据,并添加一些很好的Twitter引导CSS,如这个youtube视频所示:http://www.youtube.com/watch?v=PWI1PIvy4A8
您能帮我处理XPath代码块吗?这样我就可以刮掉数据并打印数组了吗?
发布于 2013-03-07 05:01:16
require 'nokogiri'
require 'rest-client'
url = 'http://www.1001tracklists.com/tracklist/25122_lange-intercity-podcast-115-2013-03-06.html'
page = Nokogiri::HTML(RestClient.get(url,:user_agent=>'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'), nil, 'UTF-8');
page.css('table.detail tr.tlpItem').each do |row|
artist = row.css('meta[@itemprop="byArtist"]').attr('content')
name = row.css('meta[@itemprop="name"]').attr('content')
puts "#{artist} - #{name}"
end...a更高级的版本,它从行中获取所有元信息,并打印“艺术家-宋”出版商。
require 'nokogiri'
require 'rest-client'
url = 'http://www.1001tracklists.com/tracklist/25122_lange-intercity-podcast-115-2013-03-06.html'
page = Nokogiri::HTML(RestClient.get(url,:user_agent=>'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'), nil, 'UTF-8');
page.css('table.detail tr.tlpItem').each do |row|
meta = row.search('meta').each_with_object({}) do |tag, hash|
hash[tag['itemprop']] = tag['content']
end
puts "#{meta['byArtist']} - #{meta['name']} [#{meta['publisher']||'Unknown'}]"
end你就能得到剩下的房产的照片。您需要做一些错误/存在吗?检查,因为有些歌曲没有所有的属性。但这应该能让你走上正确的轨道。我也使用了rest-client创业板,所以您可以随意使用任何您想要检索的页面。
发布于 2013-03-07 05:02:13
下面是一些代码,用于将信息收集到散列数组中。
我更喜欢使用CSS访问器,而不是XPath,因为如果您有任何HTML/CSS或jQuery经验,它们更容易读懂。
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.1001tracklists.com/tracklist/25122_lange-intercity-podcast-115-2013-03-06.html'))
data = doc.search('tr.tlpItem div[itemtype="http://schema.org/MusicRecording"]').each_with_object([]) do |div, array|
hash = div.search('meta').each_with_object({}) do |m, h|
h[m['itemprop']] = m['content']
end
link = div.at('span a')
hash['tracklistTrack'] = [ link['href'], link.text ]
title = div.at('span.floatL a')
hash['title'] = [title['href'], title.text ]
array << hash
end
pp data[0, 2]它输出页面数据的子集。经过一些按摩后,结构如下所示:
[
{
"byArtist"=>"Markus Schulz",
"name"=>"The Spiritual Gateway (Transmission 2013 Theme)",
"publisher"=>"COLDHARBOUR RECORDINGS",
"url"=>"/track/108928_markus-schulz-the-spiritual-gateway-transmission-2013-theme/index.html",
"tracklistTrack"=>[
"/track/108928_markus-schulz-the-spiritual-gateway-transmission-2013-theme/index.html",
"Markus Schulz - The Spiritual Gateway (Transmission 2013 Theme)"
],
"title"=>[
"/track/108928_markus-schulz-the-spiritual-gateway-transmission-2013-theme/index.html",
"Markus Schulz - The Spiritual Gateway (Transmission 2013 Theme)"
]
},
{
"byArtist"=>"Lange & Audrey Gallagher",
"name"=>"Our Way Home (Noah Neiman Remix)",
"publisher"=>"LANGE RECORDINGS",
"url"=>"/track/119667_lange-audrey-gallagher-our-way-home-noah-neiman-remix/index.html",
"tracklistTrack"=>[
"/track/119667_lange-audrey-gallagher-our-way-home-noah-neiman-remix/index.html",
"Lange & Audrey Gallagher - Our Way Home (Noah Neiman Remix)"
],
"title"=>[
"/track/119667_lange-audrey-gallagher-our-way-home-noah-neiman-remix/index.html",
"Lange & Audrey Gallagher - Our Way Home (Noah Neiman Remix)"
]
}
]发布于 2014-04-04 16:44:53
这里有一个免费的webservice ()从给定的url中抓取所有的400+ schema.org类,并将它们作为JSON返回。
http://scrappy.netfluid.org/
https://stackoverflow.com/questions/15262997
复制相似问题