r/dailyprogrammer • u/jnazario 2 0 • Nov 17 '17
[2017-11-17] Challenge #340 [Hard] Write a Web Crawler
Description
Most of us are familiar with web spiders and crawlers like GoogleBot - they visit a web page, index content there, and then visit outgoing links from that page. Crawlers are an interesting technology with continuing development.
Web crawlers marry queuing and HTML parsing and form the basis of search engines etc. Writing a simple crawler is a good exercise in putting a few things together. Writing a well behaved crawler is another step up.
For this challenge you may use any single shot web client you wish, e.g. Python's httplib or any of a number of libcurl bindings; you may NOT use a crawling library like Mechanize or whatnot. You may use an HTML parsing library like BeautifulSoup; you may NOT use a headless browser like PhantomJS. The purpose of this challenge is to tie together fetching a page, reassembling links, discovering links and assembling them, adding them to a queue, managing the depth of the queue, and visiting them in some reasonable order - while avoiding duplicate visits.
Your crawler MUST support the following features:
- HTTP/1.1 client behaviors
- GET requests are the only method you must support
- Parse all links presented in HTML - anchors, images, scripts, etc
- Take at least two options - a starting (seed) URL and a maximum depth to recurse to (e.g. "1" would be fetch the HTML page and all resources like images and script associated with it but don't visit any outgoing anchor links; a depth of "2" would visit the anchor links found on that first page only, etc ...)
- Do not visit the same link more than once per session
Optional features include HTTPS support, support for robots.txt, support for domains to which you restrict the crawler, and storing results (for example how wget does so).
Be careful with what you crawl! Don't get yourself banned from the Internet. I highly suggest you crawl a local server you control as you may trigger rate limits and other mechanisms to identify unwanted visitors.
19
u/jnazario 2 0 Nov 17 '17
these notes may prove useful, even if you're not using the elixir language. Learning to Crawl - Building a Bare Bones Web Crawler with Elixir
4
u/Trendamyr Nov 17 '17
What is different between a well behaved web crawler and a not well behaved one
7
u/zqvt Nov 19 '17 edited Nov 19 '17
Haskell
4
u/[deleted] Nov 17 '17 edited Nov 18 '17
Ruby
Interested in hearing any feedback on this one. I gave it the ole college-try. Hopefully I didn't create a terribly behaved monster.
Edit: I've created a monster! D: Changed the way image urls are handled and cleaned up some things. Creating a Spider object with a start url and # of steps starts the crawl. Output lists all links collected sans images, visited shows all links the spider has entered, and images shows all the image urls that have been collected.
Code:
Example:
4
u/rchenxi Nov 18 '17
Long, long time ago i have written a simple parser to download all of the zips from sqlite.org. Written in PowerShell. Enjoy :)
3
u/tomekanco Nov 18 '17 edited Nov 18 '17
Python 3.6
Not certain if using requests is allowed, I only use it for single shots.
Surprised by the output, mostly local news sites.
Output
3
u/g00glen00b Nov 20 '17
Java 8
Model class:
This will fetch all pages using JSoup. The result is a list of objects containing the URL, the title, the first 5000 characters of the content, and a list of links embedded on the page.
I also made a scheduler and a REST API using Spring.
Persistence:
Scheduler:
REST API:
Output:
3
u/schwarzfahrer Nov 22 '17
Node.js with request, cheerio, and async
Similar to /u/FunWithCthulhu3 's Ruby solution, but with async wrangling of HTTP requests.
No respect for robots.txt or rate limiting, so I'm afraid to test this anywhere but locally. Any suggestions on a good way to go about this? Currently, I just have a handful of static files that contain links to each other (at various levels of nesting), and spin up a local server to run requests against.
2
u/[deleted] Nov 17 '17
Interesting idea! I've just recently started working on a network forensics tool in Rust, and website enumeration is definitely on the todo list. Here we go...
// code here later :)
2
u/jonas77 Nov 19 '17 edited Nov 19 '17
Simple Async crawler in .NET core / C# https://gist.github.com/icanhasjonas/8a591e9ea0c955f273687ad888adf56a
1
u/mochancrimthann Jan 03 '18 edited Jan 03 '18
Node/Javascript
It will try to respect robots.txt. I wanted to use as few external dependencies as I possibly could; the only one that isn't included in node is
parse5. I want to keep working on this but I found it a bit exhausting after a while. :-) This has only been tested against a local server and http://httpbin.org.1
u/I-Downloaded-a-Car Jan 05 '18
Funnily enough I actually used to make these all the time. Mostly for statistic and art projects. Never actually considered it to be a hard challenge though.
1
u/juanchi35 Jan 07 '18 edited Jan 07 '18
Ruby
0
u/[deleted] Nov 17 '17
[deleted] — view deleted comment
0
u/[deleted] Nov 18 '17
[deleted] — view deleted comment