|
1 |
| -<?php |
2 |
| -namespace szdk\PHPWebCrawler; |
3 |
| - |
4 |
| - |
5 |
| -class Crawler |
6 |
| -{ |
7 |
| - use Queue; |
8 |
| - use Links; |
9 |
| - |
10 |
| - public $url; //url of webpage |
11 |
| - //public $rootDir = null; //top level dir (eg: https://example.com, root/) |
12 |
| - public $depth = 100; //scrap max 100 pages (put 0 for no limit) |
13 |
| - public $onlyChildren = true; //process only children urls? |
14 |
| - public $exceptions = []; //dont search through these urls, or there children directories |
15 |
| - |
16 |
| - private const REMOVE_ANCHOR = 1; |
17 |
| - private const REMOVE_QUERY = 2; |
18 |
| - private const REMOVE_SCHEME = 4; |
19 |
| - private const REMOVE_FILE_NAME = 8; |
20 |
| - private const LOWERCASE = 16; |
21 |
| - |
22 |
| - private $localFile = false; |
23 |
| - private $processedCount = 0; |
24 |
| - private $processed = []; |
25 |
| - private $queued = []; |
26 |
| - private $dirs = [ |
27 |
| - 'temp' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR, |
28 |
| - ]; |
29 |
| - private $files = [ |
30 |
| - 'processed' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'processed.json', |
31 |
| - 'queued' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queued.json', |
32 |
| - ]; |
33 |
| - |
34 |
| - public function __construct(String $url, $clearURLs = false) |
35 |
| - { |
36 |
| - $this->url = \trim($url); |
37 |
| - if (\stripos($this->url, 'http://') !== 0 && \stripos($this->url, 'https://') !== 0) { |
38 |
| - $this->localFile = true; |
39 |
| - $this->url = \str_replace('\\', '/', \realpath($this->url)); |
40 |
| - } |
41 |
| - foreach ($this->dirs as $dir) { |
42 |
| - if (!\is_dir($dir)) { |
43 |
| - @\mkdir($dir); |
44 |
| - } |
45 |
| - } |
46 |
| - if ($clearURLs) { |
47 |
| - $this->clearURLs(); |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - /* |
52 |
| - *the callable function passed must accept a webpage contents in first parameter |
53 |
| - */ |
54 |
| - public function run(Callable $fnc) |
55 |
| - { |
56 |
| - $this->queued($this->url); |
57 |
| - $this->url = $this->getTrimmedURL($this->url, self::REMOVE_ANCHOR | self::REMOVE_FILE_NAME); |
58 |
| - |
59 |
| - |
60 |
| - while (!empty($this->queued) && ($this->depth === 0 || $this->processedCount < $this->depth)) { |
61 |
| - $url = \array_key_first($this->queued); |
62 |
| - |
63 |
| - if ($this->isURLProcessed($url)) { |
64 |
| - $this->processed($url); |
65 |
| - continue; |
66 |
| - } |
67 |
| - |
68 |
| - $this->processed($url); |
69 |
| - |
70 |
| - |
71 |
| - if ($this->onlyChildren && !$this->isChildrenURL($url, $this->url)) { |
72 |
| - continue; |
73 |
| - } |
74 |
| - |
75 |
| - foreach ($this->exceptions as $link) { |
76 |
| - if ($this->isChildrenURL($url, $link)); |
77 |
| - continue; |
78 |
| - } |
79 |
| - |
80 |
| - |
81 |
| - try { |
82 |
| - $content = \file_get_contents(!$this->localFile ? $url : $this->getTrimmedURL($url, self::REMOVE_ANCHOR | self::REMOVE_QUERY)); |
83 |
| - } catch (\Exception $e) { |
84 |
| - //Logger::log($e); |
85 |
| - } |
86 |
| - |
87 |
| - $fnc(['content' => $content, 'url' => $url]); //run user generated scraping function |
88 |
| - |
89 |
| - $newLinks = $this->extractLinks($content, $url); |
90 |
| - foreach ($newLinks as $link) { |
91 |
| - if (!$this->isURLProcessed($link) && (!$this->onlyChildren || $this->isChildrenURL($link, $this->url))) { |
92 |
| - $this->queued(!$this->localFile ? $link : $this->getTrimmedURL($link, self::REMOVE_ANCHOR | self::REMOVE_QUERY)); |
93 |
| - } |
94 |
| - } |
95 |
| - $this->processedCount++; |
96 |
| - } |
97 |
| - return true; |
98 |
| - } |
99 |
| - |
100 |
| - public function clearURLs() |
101 |
| - { |
102 |
| - foreach ($this->files as $file) { |
103 |
| - if (\is_readable($file)) { |
104 |
| - @\unlink($file); |
105 |
| - } |
106 |
| - } |
107 |
| - $this->processed = $this->queued = []; |
108 |
| - } |
109 |
| -} |
110 |
| - |
| 1 | +<?php |
| 2 | +namespace szdk\PHPWebCrawler; |
| 3 | + |
| 4 | + |
| 5 | +class Crawler |
| 6 | +{ |
| 7 | + use Queue; |
| 8 | + use Links; |
| 9 | + |
| 10 | + public $url; //url of webpage |
| 11 | + //public $rootDir = null; //top level dir (eg: https://example.com, root/) |
| 12 | + public $depth = 100; //scrap max 100 pages (put 0 for no limit) |
| 13 | + public $onlyChildren = true; //process only children urls? |
| 14 | + public $exceptions = []; //dont search through these urls, or there children directories |
| 15 | + |
| 16 | + private const REMOVE_ANCHOR = 1; |
| 17 | + private const REMOVE_QUERY = 2; |
| 18 | + private const REMOVE_SCHEME = 4; |
| 19 | + private const REMOVE_FILE_NAME = 8; |
| 20 | + private const LOWERCASE = 16; |
| 21 | + |
| 22 | + private $localFile = false; |
| 23 | + private $processedCount = 0; |
| 24 | + private $processed = []; |
| 25 | + private $queued = []; |
| 26 | + private $dirs = [ |
| 27 | + 'temp' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR, |
| 28 | + ]; |
| 29 | + private $files = [ |
| 30 | + 'processed' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'processed.json', |
| 31 | + 'queued' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queued.json', |
| 32 | + ]; |
| 33 | + |
| 34 | + public function __construct(String $url, $clearURLs = false) |
| 35 | + { |
| 36 | + $this->url = \trim($url); |
| 37 | + if (\stripos($this->url, 'http://') !== 0 && \stripos($this->url, 'https://') !== 0) { |
| 38 | + $this->localFile = true; |
| 39 | + $this->url = \str_replace('\\', '/', \realpath($this->url)); |
| 40 | + } |
| 41 | + foreach ($this->dirs as $dir) { |
| 42 | + if (!\is_dir($dir)) { |
| 43 | + @\mkdir($dir); |
| 44 | + } |
| 45 | + } |
| 46 | + if ($clearURLs) { |
| 47 | + $this->clearURLs(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /* |
| 52 | + *the callable function passed must accept a webpage contents in first parameter |
| 53 | + */ |
| 54 | + public function run(Callable $fnc) |
| 55 | + { |
| 56 | + $this->queued($this->url); |
| 57 | + $this->url = $this->getTrimmedURL($this->url, self::REMOVE_ANCHOR | self::REMOVE_FILE_NAME); |
| 58 | + |
| 59 | + |
| 60 | + while (!empty($this->queued) && ($this->depth === 0 || $this->processedCount < $this->depth)) { |
| 61 | + $url = \array_key_first($this->queued); |
| 62 | + |
| 63 | + if ($this->isURLProcessed($url)) { |
| 64 | + $this->processed($url); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $this->processed($url); |
| 69 | + |
| 70 | + |
| 71 | + if ($this->onlyChildren && !$this->isChildrenURL($url, $this->url)) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($this->exceptions as $link) { |
| 76 | + if ($this->isChildrenURL($url, $link)); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + try { |
| 82 | + $content = \file_get_contents(!$this->localFile ? $url : $this->getTrimmedURL($url, self::REMOVE_ANCHOR | self::REMOVE_QUERY)); |
| 83 | + } catch (\Exception $e) { |
| 84 | + //Logger::log($e); |
| 85 | + } |
| 86 | + |
| 87 | + $fnc(['content' => $content, 'url' => $url]); //run user generated scraping function |
| 88 | + |
| 89 | + $newLinks = $this->extractLinks($content, $url); |
| 90 | + foreach ($newLinks as $link) { |
| 91 | + if (!$this->isURLProcessed($link) && (!$this->onlyChildren || $this->isChildrenURL($link, $this->url))) { |
| 92 | + $this->queued(!$this->localFile ? $link : $this->getTrimmedURL($link, self::REMOVE_ANCHOR | self::REMOVE_QUERY)); |
| 93 | + } |
| 94 | + } |
| 95 | + $this->processedCount++; |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + public function clearURLs() |
| 101 | + { |
| 102 | + foreach ($this->files as $file) { |
| 103 | + if (\is_readable($file)) { |
| 104 | + @\unlink($file); |
| 105 | + } |
| 106 | + } |
| 107 | + $this->processed = $this->queued = []; |
| 108 | + } |
| 109 | +} |
| 110 | +
|
0 commit comments