Advertisement
Guest User

Simple PHP Webscraping Simple-DOM Error

a guest
Apr 6th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. ---HTML Source to Be scraped:---
  2.     <div id="latest-stats" class="row section--wrapper three text-center">
  3.       <div class="col-xs-12">
  4.         <h2>Current Situation in Florida</h2>
  5.         <p class="sml">Updated as of Mon 4/6/2020 06:02 PM ET</p>
  6.       </div>
  7.       <div class="situation__boxes-wrapper">
  8.         <div class="col-xs-12 col-md-8 outer--box outer--box--left">
  9.           <div class="inner--box">
  10.             <h4>Positive Cases of COVID-19 in Florida</h4>
  11.                  <div class="stat--box">
  12.  <!-- TRYING TO GET THIS plaintext value 13214-->            <h2>13214</h2>
  13.                   <p>Confirmed Cases in Florida Residents</p>
  14.                 </div>
  15.                 <div class="stat--box">
  16.                <!--THIS VALUE-->   <h2>415</h2>
  17.                   <p>Cases in Non-Florida Residents</p>
  18.                 </div>
  19.                 <div class="stat--box">
  20.        <!--AND THIS VALUE-->           <h2>13629</h2>
  21.                   <p>Total Cases Overview</p>
  22.                 </div>
  23.                 </div>
  24.         </div>
  25.  
  26. ---PHP CODE SO FAR---
  27.  
  28.  
  29. <?php
  30. include('simple_html_dom.php');
  31. $html = file_get_html('https://floridahealthcovid19.gov/#lateststats');
  32.  
  33. //echo $html->find('title', 0)->plaintext;
  34.  
  35.  
  36.  
  37.  
  38. $list = $html->find('div[class="stat--box"]');
  39.  
  40. //First Method i'm trying to get to work
  41. foreach( $list->find('h2') as $element) {
  42.   echo $element->plaintext;
  43.   echo "<br />";
  44. }
  45.  
  46. //Second method I'm saving JIC
  47. //$list_array = $list->find('h2');
  48.  
  49. /*for($i = 0; $i < sizeof($list_array); $i++  ) {
  50.   echo $list_array[$i];
  51.   echo "<br />";
  52. }
  53. */
  54.  
  55.  
  56. ?>
  57.  
  58. ---I'M USING SIMPLE-DOM-PARSER---
  59. I'm not even close to a final solution but can't even seem to loop through the child <h2> tags within the div class ="stat--box"> with an exception being thrown:
  60.  
  61. Fatal error: Uncaught Error: Call to a member function find() on array in D:\xampp\htdocs\flcovid\web_scraper.php:19 Stack trace: #0 {main} thrown in D:\xampp\htdocs\flcovid\web_scraper.php on line 19
  62.  
  63. Can someone point me in the right direction? I've found plenty of tutorials and walk-throughs with respect to getting the values in top-level tags but with respect to good examples digging into nested child element values I haven't had any luck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement