【发布时间】:2022-11-12 12:39:55
【问题描述】:
一年多来,我一直在使用 watir 从 nfl.com 抓取数据,但它突然停止工作。看来这是一个时间问题,并且在 json 数据完成加载动态内容之前加载站点。我很确定这是我试图加载网站的方式,但我已经把头撞在墙上几天了,没有答案。
这是我的相关 Gemfile.lock 信息:
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
watir (7.1.0)
regexp_parser (>= 1.2, < 3)
selenium-webdriver (~> 4.0)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webdrivers (5.2.0)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0)
selenium-webdriver (4.5.0)
childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
Chrome 驱动程序版本为
ChromeDriver 90.0.4430.212 (e3cd97fc771b893b7fd1879196d1215b622c2bed-refs/branch-heads/4430@{#1429})
这是我用来调用 watir 和 nokogiri 的代码:
if Rails.env.production?
args = ['--no-sandbox', '--disable-dev-shm-usage', '--disable-gpu',
'--remote-debugging-port=9222']
browser = Watir::Browser.new :chrome, headless: true, options: {args: args}
else
browser = Watir::Browser.new :chrome, headless: true
end
url_path = "http://www.nfl.com/schedules/" + year + "/REG" + weekNum.to_s
browser.goto(url_path)
js_doc = browser.main(id: "main-content").wait_until(&:present?)
doc = Nokogiri::HTML(js_doc.inner_html)
这是一个关于我如何解析 nokogiri 数据的示例,该数据现在总是返回空白。它曾经工作过。:
game_date = game_group.css('h2.d3-o-section-title').text
最后,这是您在 Chrome 内部进行检查时获得的 HTML。我关心的 HTML 位于 .
<main role="main" id="main-content" tabindex="0" aria-label="Page main content">
<section class="d3-l-section-row d3-l-section-row--no-margin-top">... </section>
<section class="d3-l-section-row">...</section>
<section class="d3-l-grid--outer d3-l-section-row">...</section>
<section class="d3-l-grid--outer d3-l-adv-row">...</section>
<div data-json-module="{";Name";:";Schedules";,";Module";:{";seasonFromUrl";:2022,";SeasonType";:";REG9";,";WeekFromUrl";:9,";HeaderCountryCode";:";US";,";TimeZoneID";:";America/Chicago";,";PreSeasonPlacement";:0,";RegularSeasonPlacement";:0,";PostSeasonPlacement";:0}}" data-require="modules/scheduleByWeek" data-require-loaded="true">
<span style="display:none">...</span>
<section class="d3-l-grid--outer d3-l-section-row nfl-o-matchup-group">
<div class="d3-l-grid--inner">
<div class="d3-l-col__col-12">
<h2 class="d3-o-section-title">Thursday, November 3rd</h2>
<div class="nfl-c-matchup-strip nfl-c-matchup-strip--post-game">
<a class="nfl-c-matchup-strip__left-area" href="/games/eagles-at-texans-2022-reg-9" aria-label="Texans vs Eagles game page">
<div class="nfl-c-matchup-strip__game-info">
<p class="nfl-c-matchup-strip__period">FINAL</p>
</div>
<div class="nfl-c-matchup-strip__game">
<div class="nfl-c-matchup-strip__team nfl-c-matchup-strip__team--opponent">
<div class="nfl-c-matchup-strip__team-score" data-require="modules/displayScores" data-score="29" data-require-loaded="true">29</div>
<p class="nfl-c-matchup-strip__team-name">
<span class="nfl-c-matchup-strip__team-logo">
<picture>... </picture>
</span>
<span class="nfl-c-matchup-strip__team-abbreviation"> PHI </span>
<span class="nfl-c-matchup-strip__team-fullname"> Eagles </span>
</p>
<div class="nfl-c-matchup-strip__record">(8-0)</div>
</div>
<div class="nfl-c-matchup-strip__team-separator">
<span class="nfl-o-icon nfl-o-icon--medium">... </span>
</div>
<div class="nfl-c-matchup-strip__team">
<div class="nfl-c-matchup-strip__team-score" data-require="modules/displayScores" data-score="17" data-require-loaded="true">17</div>
<p class="nfl-c-matchup-strip__team-name">
<span class="nfl-c-matchup-strip__team-logo">...</span>
<span class="nfl-c-matchup-strip__team-abbreviation"> HOU </span>
<span class="nfl-c-matchup-strip__team-fullname"> Texans </span>
</p>
<div class="nfl-c-matchup-strip__record">(1-6-1)</div>
</div>
</div>
</a>
<div class="nfl-c-matchup-strip__right-area">
<a class="nfl-o-cta nfl-o-cta--link" href="/games/eagles-at-texans-2022-reg-9" aria-label="Replay">
<span class="nfl-o-icon nfl-o-icon--medium">...</span>
<span>Replay</span>
</a>
</div>
</div>
</div>
</div>
</section>
在过去的一年中,使用 "js_doc = browser.main(id: "main-content").wait_until(&:present?)" 等待主要内容加载,但似乎返回得太早了。我试过在 data-json-module div "js_doc = browser.div(class: "nfl-o-matchup-group")" 之后等待内容,但它总是超时。而且我不确定如何打开 .
我真的不知道为什么它工作了这么长时间,现在正在崩溃。但是,我猜我输入了错误的东西并导致了这种竞争状况,但我不知道还能尝试什么。我希望有更多wair知识的人可以帮助我。
【问题讨论】:
标签: json ruby ruby-on-rails-5 nokogiri watir