Skip to content

Instantly share code, notes, and snippets.

@javedbaloch4
Created December 4, 2020 15:59
Show Gist options
  • Save javedbaloch4/0269047ebba2bc1d53429573cf371124 to your computer and use it in GitHub Desktop.
Save javedbaloch4/0269047ebba2bc1d53429573cf371124 to your computer and use it in GitHub Desktop.
Simple Web Scrapping in PHP.
<?php
// Using the library https://simplehtmldom.sourceforge.io
include("simple_html_dom.php");
# Example 1 - Get the title of webpage.
$html = file_get_html("https://www.google.com");
// Returns title of web page.
$html->find('title' ,0)->plaintext;
# Example 2 - Get Navigation / & Other examples
$list = $html->find('div[class="w3-bar w3-left"]', 0);
/*
$list_array = $list->find('a');
echo sizeof($list_array);
for ($i = 0; $i < sizeof($list_array); $i++) {
echo $list_array[$i];
echo "<br>";
}
*/
/*
foreach ($list->find('a') as $key => $element) {
echo $key . " " .$element->plaintext;
echo "<br>";
}
*/
/*
foreach ($html->find('div') as $element) {
echo $element;
echo "<br>";
}
*/
/*
foreach ($html->find('a') as $element) {
// echo $element;
if ($element->plaintext == 'HTML' || $element->plaintext == 'CSS') {
echo $element;
}
}
*/
/*
foreach ($html->find('p') as $element) {
echo $element;
echo "<br>";
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment