gpt4 book ai didi

ruby - 我如何使用 Nokogiri 来解析 bit.ly 统计页面?

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:18 24 4
gpt4 key购买 nike

我正在尝试从 a bit.ly stats page 中解析 Twitter 用户名使用 Nokogiri:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('http://bitly.com/U026ue+/global'))

twitter_accounts = []

shares = doc.xpath('//*[@id="tweets"]/li')

shares.map do |tweet|
twitter_accounts << tweet.at_css('.conv.tweet.a')
end

puts twitter_accounts

我的理解是,Nokogiri 将以某种树结构形式保存份额,我可以使用它来深入研究,但我的里程数各不相同。

最佳答案

该数据来自带有 JSON 响应的 Ajax 请求。不过,这很容易理解:

require 'json'
url = 'http://search.twitter.com/search.json?_usragnt=Bitly&include_entities=true&rpp=100&q=nowness.com%2Fday%2F2012%2F12%2F6%2F2643'
hash = JSON.parse open(url).read
puts hash['results'].map{|x| x['from_user']}

我通过在 Chrome 中加载页面然后查看网络面板获得了该 URL,我还删除了时间戳和回调参数只是为了稍微清理一下。

关于ruby - 我如何使用 Nokogiri 来解析 bit.ly 统计页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13784672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com