- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我编写了 Ruby scraper 来从加利福尼亚州参议院获取竞选财务数据,然后将每个人保存为哈希。到目前为止,这是代码:
这是主要网站:http://cal-access.sos.ca.gov/Campaign/Candidates/
这是一个候选页面的例子:http://cal-access.sos.ca.gov/Campaign/Committees/Detail.aspx?id=1342974&session=2011&view=received
如果您想在代码中查看我的评论,这里是 github 存储库:https://github.com/aboutaaron/Baugh-For-Senate-2012/blob/master/final-exam.rb
关于代码...
require 'nokogiri'
require 'open-uri'
campaign_data = Nokogiri::HTML(open('http://cal-access.sos.ca.gov/Campaign/Candidates/'))
class Candidate
def initialize(url)
@url = url
@cal_access_url = "http://cal-access.sos.ca.gov"
@nodes = Nokogiri::HTML(open(@cal_access_url + @url))
end
def get_summary
candidate_page = @nodes
{
:political_party => candidate_page.css('span.hdr15').text,
:current_status => candidate_page.css('td tr:nth-child(2) td:nth-child(2) .txt7')[0].text,
:last_report_date => candidate_page.css('td tr:nth-child(3) td:nth-child(2) .txt7')[0].text,
:reporting_period => candidate_page.css('td tr:nth-child(4) td:nth-child(2) .txt7')[0].text,
:contributions_this_period => candidate_page.css('td tr:nth-child(5) td:nth-child(2) .txt7')[0].text.gsub(/[$,](?=\d)/, ''),
:total_contributions_this_period => candidate_page.css('td tr:nth-child(6) td:nth-child(2) .txt7')[0].text.gsub(/[$,](?=\d)/, ''),
:expenditures_this_period => candidate_page.css('td tr:nth-child(7) td:nth-child(2) .txt7')[0].text.gsub(/[$,](?=\d)/, ''),
:total_expenditures_this_period => candidate_page.css('td tr:nth-child(8) td:nth-child(2) .txt7')[0].text.gsub(/[$,](?=\d)/, ''),
:ending_cash => candidate_page.css('td tr:nth-child(9) td:nth-child(2) .txt7')[0].text.gsub(/[$,](?=\d)/, '')
}
end
def get_contributors
contributions_received = @nodes
grab_contributor_page = @nodes.css("a.sublink6")[0]['href']
contributor_page = Nokogiri::HTML(open(@cal_access_url + grab_contributor_page))
grab_contributions_page = contributor_page.css("a")[25]["href"]
contributions_received = Nokogiri::HTML(open(@cal_access_url + grab_contributions_page))
puts
puts "#{@cal_access_url}" + "#{grab_contributions_page}"
puts
contributions_received.css("table").reduce([]) do |memo, contributors|
begin
memo << {
:name_of_contributor => contributions_received.css("table:nth-child(57) tr:nth-child(2) td:nth-child(1) .txt7").text
}
rescue NoMethodError => e
puts e.message
puts "Error on #{contributors}"
end
memo
end
end
end
campaign_data.css('a.sublink2').each do |candidates|
puts "Just grabbed the page for " + candidates.text
candidate = Candidate.new(candidates["href"])
p candidate.get_summary
end
get_summary
按计划工作。 get_contributors
店铺第一投稿<td>
按计划,但做了 20 多次。在我弄清楚多重打印问题之前,我现在只选择获取名称。
最终目标是对贡献者及其所需的所有信息进行哈希处理,并可能将它们转移到 SQL 数据库/Rails 应用程序中。但是,以前,我只想要一个可以工作的刮板。
有什么建议或指导吗?抱歉,如果代码不是 super 的。 super 编程新手。
最佳答案
你做得很好。在提供独立样本方面做得很好。您会惊讶于有多少人没有这样做。
我看到两个问题。
首先,并非所有页面都有您要查找的统计信息。这会使您的解析例程变得有些困惑。为了防止这种情况,您可以将其放入 get_summary
:
return nil if candidate_page.text =~ /has not electronically filed/i
调用者在看到 nil 时应该做一些聪明的事情。
另一个问题是服务器有时响应不及时,导致脚本超时。如果您认为服务器对您的脚本发出请求的速度感到不安,您可以尝试添加一些 sleep 来减慢它的速度。或者,您可以添加重试循环。或者,您可以增加脚本超时所需的时间。
在get_summary
中也有一些重复的逻辑。此功能可能受益于策略与逻辑的分离。该策略是从页面检索哪些数据,以及如何格式化它:
FORMAT_MONEY = proc do |s|
s.gsub(/[$,](?=\d)/, '')
end
FIELDS = [
[:political_party, 'span.hdr15'],
[:current_status, 'td tr:nth-child(2) td:nth-child(2) .txt7'],
[:last_report_date, 'td tr:nth-child(3) td:nth-child(2) .txt7'],
[:reporting_period, 'td tr:nth-child(4) td:nth-child(2) .txt7'],
[:contributions_this_period, 'td tr:nth-child(5) td:nth-child(2) .txt7', FORMAT_MONEY],
[:total_contributions_this_period, 'td tr:nth-child(6) td:nth-child(2) .txt7', FORMAT_MONEY],
[:expenditures_this_period, 'td tr:nth-child(7) td:nth-child(2) .txt7', FORMAT_MONEY],
[:total_expenditures_this_period, 'td tr:nth-child(8) td:nth-child(2) .txt7', FORMAT_MONEY],
[:ending_cash, 'td tr:nth-child(9) td:nth-child(2) .txt7', FORMAT_MONEY],
]
实现是如何将该策略应用于 HTML 页面:
def get_summary
candidate_page = @nodes
return nil if candidate_page.text =~ /has not electronically filed/i
keys_and_values = FIELDS.map do |key, css_selector, format|
value = candidate_page.css(css_selector)[0].text
value = format[value] if format
[key, value]
end
Hash[keys_and_values]
end
关于ruby - 用 Ruby 抓取并存储在哈希中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11165918/
如本answer所述,如果浏览器不支持 e,可以设置后备游标。 G。 光标:抓取;。我现在的问题是获取这些图像。在我的驱动器上本地搜索“.cur”只给了我系统光标,其中 grab.cur 和 grab
以下代码在计算机上运行以从 Instagram 帐户中抓取数据。当我尝试在 VPS 服务器上使用它时,我被重定向到 Instagram 登录页面,因此脚本不起作用。 为什么当我在电脑上或服务器上时,I
我在使用 Ruby 和 Mechanize 将 POST 查询传递到站点的网站上。访问站点的查询基于 firebug,如下所示 param.PrdNo=-1¶m.Type=Prop¶m
我正在尝试抓取一个具有多个页面结果的网站,例如“1、2、3、4、5...”。 每个分页号都是到另一个页面的链接,我需要抓取每个页面。 到目前为止,我想出了这个: while lien = page.l
我正在使用 HtmlAgilityPack 在 C# Asp.Net 中执行 Scraping,到目前为止,我在从多个 Web 执行 Scratch 时没有遇到问题,但是,尝试弹出以下代码时出现错误
如果我有一个 css 文件做这样的事情 #foo:after{content:"bar;} ,有没有办法用 javascript 获取 :after 的内容?获取父元素的内容只返回 #foo 元素的内
问题是这样的: 我有一个 Web 应用程序 - 一个经常更改的通知系统 - 在一系列本地计算机上运行。该应用程序每隔几秒刷新一次以显示新信息。计算机仅显示信息,没有键盘或任何输入设备。 问题是,如果与
我想制作一个程序来模拟用户浏览网站和点击链接。必须启用 Cookie 和 javascript。我已经在 python 中成功地做到了这一点,但我想把它写成一种可编译的语言(python ide 不会
我制作了这个小机器人,它通过搜索参数列表进行处理。它工作正常,直到页面上有几个结果: product_prices_euros 给出了一半为空的项目列表。因此,当我与 product_prices_c
我需要找到一个单词的匹配项,例如: 在网上找到所有单词“学习”https://www.georgetown.edu/(结果:4个字)(您可以看到它按CTRL + F并搜索) 我有我的 Python 代
有一个站点\资源提供一些一般统计信息以及搜索工具的界面。这种搜索操作成本高昂,因此我想限制频繁且连续(即自动)的搜索请求(来自人,而不是来自搜索引擎)。 我相信有很多现有的技术和框架可以执行一些情报抓
这并不是真正的抓取,我只是想在网页中找到类具有特定值的 URL。例如: 我想获取 href 值。关于如何做到这一点的任何想法?也许正则表达式?你能发布一些示例代码吗?我猜 html 抓取库,比如 B
我正在使用 scrapy。 我正在使用的网站具有无限滚动功能。 该网站有很多帖子,但我只抓取了 13 个。 如何抓取剩余的帖子? 这是我的代码: class exampleSpider(scrapy.
我正在尝试从这个 website 中抓取图像和新闻 url .我定义的标签是 root_tag=["div", {"class":"ngp_col ngp_col-bottom-gutter-2 ng
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭上个月。 Improve this ques
我在几个文件夹中有数千个 html 文件,我想从评论中提取数据并将其放入 csv 文件中。这将允许我为项目格式化和清理它。例如,我在这个文件夹中有 640 个 html 文件: D:\My Web S
我在编写用于抓取网页的实用程序时遇到了一个问题。 我正在发送 POST 请求来检索数据,我模仿我正在抓取的网络行为(根据使用 fiddler 收集的信息)。 我已经能够自动替换我的 POST 中除 V
对于 Googlebot 的 AJAX 抓取,我在我的网站中使用“_escaped_fragment_”参数。 现在我查看了 Yandex 对我网站的搜索结果。 我看到搜索结果中不存在 AJAX 响应
我正在尝试抓取网站的所有结果页面,它可以工作,但有时脚本会停止并显示此错误: 502 => Net::HTTPBadGateway for https://website.com/id/12/ --
我是一个学习网络爬虫的初学者,由于某种原因我无法爬网this地点。当我在 Chrome 中检查它时,代码看起来不错,但是当我用 BeautifulSoup 阅读它时,它不再是可刮的。汤提到“谷歌分析”
我是一名优秀的程序员,十分优秀!