nil:NilClass的未定义方法“单击"(机械化) [英] Undefined method 'click' for nil:NilClass (Mechanize)
本文介绍了nil:NilClass的未定义方法“单击"(机械化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Mechanize构建脚本来从网站抓取数据.该脚本应该单击阅读传记"链接,然后在下一页上抓取该成员的传记.
I am building a script using Mechanize to scrape data from a website. The script is supposed to click on the "Read biography" link and then scrape the biography of the member on the next page.
这是Rake文件中的脚本:
Here is the script in the Rake file:
require 'mechanize'
require 'date'
require 'json'
task :testing2 do
agent = Mechanize.new
page = agent.get("https://www.congress.gov/members")
page_links = page.links_with(href: %r{.*/member/\w+})
member_links = page_links[0...2]
members = member_links.map do |link|
member = link.click
name = member.search('title').text.split('|')[0]
institution = member.search('td~ td+ td').text.split(':')[0]
dob = member.search('.birthdate').text.strip[1..4]
# Get bio
bio_link = member.link_with(:text => 'Read biography').click
bio = bio_page.search('p').text.strip
{
name: name.strip,
institution: institution.strip,
dob: dob,
bio: bio
}
end
puts JSON.pretty_generate(members)
end
推荐答案
您正在使用的代码:
member.link_with(:text => 'Read biography')
找不到链接,因为链接中有一些空格和换行符.您需要像这样使用它:
does not find the link, because the link has some space and new lines characters in it. You need to use it like this:
member.link_with(:text => /Read biography/)
该代码将找到链接.
这篇关于nil:NilClass的未定义方法“单击"(机械化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文