Skip to content

Commit 57de24f

Browse files
committed
Updated readme and example.
1 parent 318132c commit 57de24f

3 files changed

Lines changed: 30 additions & 24 deletions

File tree

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ A very useful web crawler for vulnerability scanning. Not Your Average Web Crawl
1919

2020
**Current limitations:**
2121
- Only works on Python 3.6 or higher.
22-
- Multiprocessing is not yet working.
23-
- Maximum recursion depth exception when crawling too much resources.
22+
- Ignore similar requests option not working.
2423

2524
**Future development:**
26-
2725
- Fix current limitations.
2826
- Performance improvements.
2927
- Support XHR crawling.
@@ -40,6 +38,7 @@ First make sure you're on [Python 3.6](https://www.python.org/) or higher. Then
4038
## Example usage
4139
```python
4240
from nyawc.Options import Options
41+
from nyawc.Queue import QueueItem
4342
from nyawc.Crawler import Crawler, CrawlerActions
4443
from nyawc.http.Request import Request
4544

@@ -50,18 +49,19 @@ def cb_crawler_after_finish(queue):
5049
print("Crawler finished. Found " + str(queue.get_count()) + " requests.")
5150

5251
for queue_item in queue.get_all():
53-
print(queue_item.request.method + ": " + queue_item.request.url + " (" + str(queue_item.request.data) + ")")
52+
print("[" + queue_item.request.method + "] " + queue_item.request.url + " (PostData: " + str(queue_item.request.data) + ")")
5453

5554
def cb_request_before_start(queue, queue_item):
5655
# return CrawlerActions.DO_SKIP_TO_NEXT
5756
# return CrawlerActions.DO_STOP_CRAWLING
57+
5858
return CrawlerActions.DO_CONTINUE_CRAWLING
5959

6060
def cb_request_after_finish(queue, queue_item, new_queue_items):
6161
percentage = str(int(queue.get_progress()))
6262
total_requests = str(queue.get_count())
6363

64-
print("At " + percentage + "% of " + total_requests + " requests (" + queue_item.request.url + ").")
64+
print("At " + percentage + "% of " + total_requests + " requests ([" + str(queue_item.response.status_code) + "] " + queue_item.request.url + ").")
6565

6666
# return CrawlerActions.DO_STOP_CRAWLING
6767
return CrawlerActions.DO_CONTINUE_CRAWLING
@@ -77,13 +77,13 @@ options.callbacks.request_after_finish = cb_request_after_finish # Called after
7777

7878
# Scope options
7979
options.scope.protocol_must_match = False # Only crawl pages with the same protocol as the startpoint (e.g. only https). Default is False.
80-
options.scope.subdomain_must_match = True # Only crawl pages with the same subdomain as the startpoint. If the startpoint is not a subdomain, no subdomains will be crawled. Default is True.
80+
options.scope.subdomain_must_match = False # Only crawl pages with the same subdomain as the startpoint. If the startpoint is not a subdomain, no subdomains will be crawled. Default is True.
8181
options.scope.domain_must_match = True # Only crawl pages with the same domain as the startpoint (e.g. only finnwea.com). Default is True.
8282
options.scope.ignore_similar_requests = True # Ignore similar requests like `?page=1` & `?page=2` or `/page/1` and `/page/2`. Default is True.
8383
options.scope.max_depth = None # The maximum search depth. For example, 2 would be the startpoint and all the pages found on it. Default is None (unlimited).
8484

8585
# Performance options
86-
options.performance.max_processes = 8 # The maximum amount of simultaneous processes to use for crawling. Default is 8.
86+
options.performance.max_threads = 8 # The maximum amount of simultaneous threads to use for crawling. Default is 4.
8787

8888
crawler = Crawler(options)
8989
crawler.start_with(Request("https://finnwea.com/"))

example.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@
2727
from nyawc.Crawler import Crawler, CrawlerActions
2828
from nyawc.http.Request import Request
2929

30-
import sys
31-
3230
def cb_crawler_before_start():
3331
print("Crawler started.")
3432

3533
def cb_crawler_after_finish(queue):
3634
print("Crawler finished. Found " + str(queue.get_count()) + " requests.")
3735

3836
for queue_item in queue.get_all():
39-
print(queue_item.request.method + ": " + queue_item.request.url + " (" + str(queue_item.request.data) + ")")
37+
print("[" + queue_item.request.method + "] " + queue_item.request.url + " (PostData: " + str(queue_item.request.data) + ")")
4038

4139
def cb_request_before_start(queue, queue_item):
4240
# return CrawlerActions.DO_SKIP_TO_NEXT
@@ -48,7 +46,7 @@ def cb_request_after_finish(queue, queue_item, new_queue_items):
4846
percentage = str(int(queue.get_progress()))
4947
total_requests = str(queue.get_count())
5048

51-
print("At " + percentage + "% of " + total_requests + " requests (" + str(queue_item.response.status_code) + "/" + queue_item.request.url + ").")
49+
print("At " + percentage + "% of " + total_requests + " requests ([" + str(queue_item.response.status_code) + "] " + queue_item.request.url + ").")
5250

5351
# return CrawlerActions.DO_STOP_CRAWLING
5452
return CrawlerActions.DO_CONTINUE_CRAWLING
@@ -70,7 +68,7 @@ def cb_request_after_finish(queue, queue_item, new_queue_items):
7068
options.scope.max_depth = None # The maximum search depth. For example, 2 would be the startpoint and all the pages found on it. Default is None (unlimited).
7169

7270
# Performance options
73-
options.performance.max_threads = 16 # The maximum amount of simultaneous threads to use for crawling. Default is 4.
71+
options.performance.max_threads = 8 # The maximum amount of simultaneous threads to use for crawling. Default is 4.
7472

7573
crawler = Crawler(options)
76-
crawler.start_with(Request("https://tweakers.net/"))
74+
crawler.start_with(Request("https://finnwea.com/"))

nyawc/Crawler.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,26 +212,40 @@ def __request_finish(self, queue_item, new_requests):
212212
if action == CrawlerActions.DO_CONTINUE_CRAWLING or action is None:
213213
return self.__spawn_new_requests()
214214

215+
class CrawlerThread(threading.Thread):
216+
"""The crawler thread executes the HTTP request using the HTTP handler.
215217
218+
Attributes:
219+
__callback (obj): The method to call when finished
220+
__callback_lock (bool): The callback lock that prevents race conditions.
221+
__queue_item (obj): The queue item containing a request to execute.
216222
223+
"""
217224

225+
__callback = None
218226

227+
__callback_lock = None
219228

220-
class CrawlerThread(threading.Thread):
229+
__queue_item = None
221230

222-
__callback = None
231+
def __init__(self, callback, callback_lock, queue_item):
232+
"""Constructs a crawler thread class
223233
224-
__queue_item = None
234+
Args:
235+
callback (obj): The method to call when finished
236+
callback_lock (bool): The callback lock that prevents race conditions.
237+
queue_item (obj): The queue item containing a request to execute.
225238
226-
__callback_lock = None
239+
"""
227240

228-
def __init__(self, callback, callback_lock, queue_item):
229241
threading.Thread.__init__(self)
230242
self.__callback = callback
231243
self.__queue_item = queue_item
232244
self.__callback_lock = callback_lock
233245

234246
def run(self):
247+
"""Executes the HTTP call"""
248+
235249
try:
236250
handler = Handler(self.__queue_item)
237251
self.__queue_item.response.raise_for_status()
@@ -247,12 +261,6 @@ def run(self):
247261
with self.__callback_lock:
248262
self.__callback(self.__queue_item, new_requests)
249263

250-
251-
252-
253-
254-
255-
256264
class CrawlerActions:
257265
"""The actions that crawler callbacks can return.
258266

0 commit comments

Comments
 (0)