Technical Documentation¶
darc is designed as a swiss army knife for darkweb crawling.
It integrates requests to collect HTTP request and response
information, such as cookies, header fields, etc. It also bundles
selenium to provide a fully rendered web page and screenshot
of such view.
As the websites can be sometimes irritating for their anti-robots
verification, login requirements, etc., the darc project
also privides hooks to customise crawling behaviours around both
requests and selenium.
See also
Such customisation, as called in the darc project, site
hooks, is site specific, user can set up your own hooks unto a
certain site, c.f. darc.sites for more information.
Still, since the network is a world full of mysteries and miracles,
the speed of crawling will much depend on the response speed of
the target website. To boost up, as well as meet the system capacity,
the darc project introduced multiprocessing, multithreading
and the fallback slowest single-threaded solutions when crawling.
Note
When rendering the target website using selenium powered by
the renown Google Chrome, it will require much memory to run.
Thus, the three solutions mentioned above would only toggle the
behaviour around the use of selenium.
To keep the darc project as it is a swiss army knife, only the
main entrypoint function darc.process.process() is exported
in global namespace (and renamed to darc.darc()), see below:
-
darc.darc(worker)¶ Main process.
The function will register
_signal_handler()forSIGTERM, and start the main process of thedarcdarkweb crawlers.- Parameters
worker (Literal[crawler, loader]) – Worker process type.
- Raises
ValueError – If
workeris not a valid value.- Return type
Before starting the workers, the function will start proxies through
darc.proxy.tor.tor_proxy()darc.proxy.i2p.i2p_proxy()darc.proxy.zeronet.zeronet_proxy()darc.proxy.freenet.freenet_proxy()
The general process can be described as following for workers of
crawlertype:process_crawler(): obtain URLs from therequestslink database (c.f.load_requests()), and feed such URLs tocrawler().crawler(): parse the URL usingparse_link(), and check if need to crawl the URL (c.f.PROXY_WHITE_LIST,PROXY_BLACK_LIST,LINK_WHITE_LISTandLINK_BLACK_LIST); if true, then crawl the URL withrequests.If the URL is from a brand new host,
darcwill first try to fetch and saverobots.txtand sitemaps of the host (c.f.save_robots()andsave_sitemap()), and extract then save the links from sitemaps (c.f.read_sitemap()) into link database for future crawling (c.f.save_requests()). Also, if the submission API is provided,submit_new_host()will be called and submit the documents just fetched.If
robots.txtpresented, andFORCEisFalse,darcwill check if allowed to crawl the URL.Note
The root path (e.g.
/in https://www.example.com/) will always be crawled ignoringrobots.txt.At this point,
darcwill call the customised hook function fromdarc.sitesto crawl and get the final response object.darcwill save the session cookies and header information, usingsave_headers().Note
If
requests.exceptions.InvalidSchemais raised, the link will be saved bysave_invalid(). Further processing is dropped.If the content type of response document is not ignored (c.f.
MIME_WHITE_LISTandMIME_BLACK_LIST),submit_requests()will be called and submit the document just fetched.If the response document is HTML (
text/htmlandapplication/xhtml+xml),extract_links()will be called then to extract all possible links from the HTML document and save such links into the database (c.f.save_requests()).And if the response status code is between
400and600, the URL will be saved back to the link database (c.f.save_requests()). If NOT, the URL will be saved intoseleniumlink database to proceed next steps (c.f.save_selenium()).
The general process can be described as following for workers of
loadertype:process_loader(): in the meanwhile,darcwill obtain URLs from theseleniumlink database (c.f.load_selenium()), and feed such URLs toloader().loader(): parse the URL usingparse_link()and start loading the URL usingseleniumwith Google Chrome.At this point,
darcwill call the customised hook function fromdarc.sitesto load and return the originalChromeobject.If successful, the rendered source HTML document will be saved, and a full-page screenshot will be taken and saved.
If the submission API is provided,
submit_selenium()will be called and submit the document just loaded.Later,
extract_links()will be called then to extract all possible links from the HTML document and save such links into therequestsdatabase (c.f.save_requests()).
After each round,
darcwill call registered hook functions in sequential order, with the type of worker ('crawler'or'loader') and the current link pool as its parameters, seeregister()for more information.If in reboot mode, i.e.
REBOOTisTrue, the function will exit after first round. If not, it will renew the Tor connections (if bootstrapped), c.f.renew_tor_session(), and start another round.See also
The function is renamed from
darc.process.process().
And we also exported the necessary hook registration functions to the global namespace, see below:
-
darc.register_hooks(hook, *, _index=None)¶ Register hook function.
- Parameters
hook (Callable[[Literal[crawler, loader], List[darc.link.Link]], None]) – Hook function to be registered.
_index (Optional[int]) –
- Keyword Arguments
_index – Position index for the hook function.
- Return type
The hook function takes two parameters:
a
strobject indicating the type of worker, i.e.'crawler'or'loader';a
listobject containingLinkobjects, as the current processed link pool.
The hook function may raises
WorkerBreakso that the worker shall break from its indefinite loop upon finishing of current round. Any value returned from the hook function will be ignored by the workers.See also
The hook functions will be saved into
_HOOK_REGISTRY.See also
The function is renamed from
darc.process.register().
-
darc.register_proxy(proxy, session=<function null_session>, driver=<function null_driver>)¶ Register new proxy type.
- Parameters
proxy (str) – Proxy type.
session (Callable[[bool], requests.sessions.Session]) – Session factory function, c.f.
darc.requests.null_session().driver (Callable[[], selenium.webdriver.chrome.webdriver.WebDriver]) – Driver factory function, c.f.
darc.selenium.null_driver().
- Return type
See also
The function is renamed from
darc.proxy.register().
-
darc.register_sites(site, *hostname)¶ Register new site map.
- Parameters
site (Type[darc.sites._abc.BaseSite]) – Sites customisation class inherited from
BaseSite.*hostname (Tuple[str]) – Optional list of hostnames the sites customisation should be registered with. By default, we use
site.hostname.
- Return type
See also
The function is renamed from
darc.sites.register().
For more information on the hooks, please refer to the customisation documentations.