【问题标题】:scraping data using Watir no longer loading JSON dynamic content from a website使用 Watir 抓取数据不再从网站加载 JSON 动态内容
【发布时间】: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


    【解决方案1】:

    当我以较慢的互联网连接加载页面时:

    • browser.main(id: "main-content") 开始存在并且
    • 在实际游戏数据出现之前存在一个微调器browser.div(class: 'nfl-o-spinner')

    我会尝试等待:

    • 要出现的游戏数据 - browser.section(class: 'nfl-o-matchup-group')
    • 找不到游戏数据 - browser.section(class: 'nfl-o-no-results')

    所以页面的加载会变成:

    browser.goto(url_path)
    browser.wait_until(timeout: 120) { browser.section(class: 'nfl-o-matchup-group').present? || browser.section(class: 'nfl-o-no-results').present? }
    js_doc = browser.main(id: 'main-content')
    doc = Nokogiri::HTML(js_doc.inner_html)
    

    【讨论】:

    • 我尝试了您的答案,但所有数据仍未加载,并且 div.nfl-o-spinner 仍然存在。有没有办法等到它不存在?这似乎是我可能需要尝试的
    • 我尝试了多次,但总是收到此错误 - “Watir::Wait::TimeoutError: 30 秒后超时,等待 #<Watir::Browser:0x..fe3d6ceb43fa87800 url="nfl.com/schedules/2022/reg1" 上的真实条件title="NFL 2022 - 第 1 周时间表 | NFL.com">"
    • 如果手动进入页面,加载游戏数据的时间是否超过30秒?鉴于仍显示微调器,请尝试增加 :timeout 以便数据有更多时间加载 - 我更新了答案以包括 2 分钟的等待。如果您想尝试等待微调器消失,您可以执行browser.div(class: 'nfl-o-spinner').wait_while(&amp;:present?),但我认为这种方法并不可靠。
    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2019-01-18
    • 2020-04-06
    相关资源
    最近更新 更多