fork download
  1. <?php
  2. function scrapeWinners($url)
  3. {
  4. // Initialize cURL session
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_URL, $url);
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  8. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  9.  
  10. // Execute cURL and get the HTML content
  11. $html = curl_exec($ch);
  12.  
  13. if (curl_errno($ch)) {
  14. echo "cURL error: " . curl_error($ch);
  15. curl_close($ch);
  16. return [];
  17. }
  18.  
  19. curl_close($ch);
  20.  
  21. // Load HTML into DOMDocument
  22. $dom = new DOMDocument();
  23. libxml_use_internal_errors(true); // Suppress HTML warnings
  24. $dom->loadHTML($html);
  25.  
  26. // Parse the table containing winners
  27. $xpath = new DOMXPath($dom);
  28. $rows = $xpath->query("//table[contains(@class, 'table')]//tr");
  29.  
  30. $winners = [];
  31. foreach ($rows as $rowIndex => $row) {
  32. // Skip the header row
  33. if ($rowIndex === 0) {
  34. continue;
  35. }
  36.  
  37. $columns = $row->getElementsByTagName('td');
  38. if ($columns->length > 0) {
  39. $winners[] = [
  40. 'constituency' => trim($columns->item(0)->textContent),
  41. 'candidate' => trim($columns->item(1)->textContent),
  42. 'party' => trim($columns->item(2)->textContent),
  43. 'votes' => trim($columns->item(3)->textContent),
  44. 'margin' => trim($columns->item(4)->textContent),
  45. 'gender' => trim($columns->item(5)->textContent),
  46. ];
  47. }
  48. }
  49.  
  50. return $winners;
  51. }
  52.  
  53. // URL to scrape
  54. $url = "https://w...content-available-to-author-only...a.info/Jharkhand2024/index.php?action=show_winners&sort=default";
  55.  
  56. // Scrape the data
  57. $winnersList = scrapeWinners($url);
  58.  
  59. // Display the results
  60. if (!empty($winnersList)) {
  61. echo "<table border='1'>";
  62. echo "<tr><th>Constituency</th><th>Candidate</th><th>Party</th><th>Votes</th><th>Margin</th><th>Gender</th></tr>";
  63. foreach ($winnersList as $winner) {
  64. echo "<tr>";
  65. echo "<td>{$winner['constituency']}</td>";
  66. echo "<td>{$winner['candidate']}</td>";
  67. echo "<td>{$winner['party']}</td>";
  68. echo "<td>{$winner['votes']}</td>";
  69. echo "<td>{$winner['margin']}</td>";
  70. echo "<td>{$winner['gender']}</td>";
  71. echo "</tr>";
  72. }
  73. echo "</table>";
  74. } else {
  75. echo "No data found or unable to scrape.";
  76. }
  77.  
Success #stdin #stdout 0.03s 26124KB
stdin
Standard input is empty
stdout
cURL error: Could not resolve host: www.myneta.infoNo data found or unable to scrape.