Crawler (AKA Spider) (AKA Robot) (AKA Bot) Some of these slides are based on Stanford IR Course slides at http://www.stanford.edu/class/cs276/
Basic crawler operation Sec. 20.2 Basic crawler operation Begin with known “seed” URLs Fetch and parse them Extract URLs they point to Place the extracted URLs on a queue Fetch each URL on the queue and repeat
Basic Crawler Frontier Will this terminate? removeURL( ) getPage( ) getLinksFromPage( )
Suppose our robot wants to download: Downloading Web Pages Suppose our robot wants to download: http://www.cs.huji.ac.il/~webdata
Got it! Now, lets parse it and continue on… www.cs.huji.ac.il/~webdata Got it! Now, lets parse it and continue on… DNS 132.68.238.21 HTTP Request HTTP Response Actually more complicated; Server replies with redirection Web Server Webdata/index.html File System 132.68.238.21
Crawling picture Web URLs frontier URLs crawled and parsed Unseen Web Sec. 20.2 Crawling picture Web URLs frontier URLs crawled and parsed Unseen Web Seed pages
Structure of the Web http://www9.org/w9cdrom/160/160.html What does this mean for seed choice?
Simple picture – complications Sec. 20.1.1 Simple picture – complications Web crawling isn’t feasible with one machine All of the above steps distributed Malicious pages Spam pages Spider traps – dynamically generated Even non-malicious pages pose challenges Latency/bandwidth to remote servers vary Webmasters’ stipulations How “deep” should you crawl a site’s URL hierarchy? Site mirrors and duplicate pages Politeness – don’t hit a server too often
What any crawler must do Sec. 20.1.1 What any crawler must do Be Polite: Respect implicit and explicit politeness considerations Only crawl allowed pages Respect robots.txt (more on this shortly) Be Robust: Be immune to spider traps and other malicious behavior from web servers
What any crawler should do Sec. 20.1.1 What any crawler should do Be capable of distributed operation: designed to run on multiple distributed machines Be scalable: designed to increase the crawl rate by adding more machines Performance/efficiency: permit full use of available processing and network resources
What any crawler should do Sec. 20.1.1 What any crawler should do Fetch pages of “higher quality” first Continuous operation: Continue fetching fresh copies of a previously fetched page Extensible: Adapt to new data formats, protocols
Updated crawling picture Sec. 20.1.1 Updated crawling picture URLs crawled and parsed Unseen Web Seed Pages URL frontier Crawling thread
Processing steps in crawling Sec. 20.2.1 Processing steps in crawling Pick a URL from the frontier Fetch the document at the URL Parse the URL Extract links from it to other docs (URLs) Check if URL has content already seen If not, add to indexes For each extracted URL Ensure it passes certain URL filter tests Check if it is already in the frontier (duplicate URL elimination) Which one? E.g., only crawl .edu, obey robots.txt, etc.
Basic crawl architecture Sec. 20.2.1 Basic crawl architecture WWW DNS URL set Doc FP’s robots filters Parse Fetch Content seen? URL filter Dup URL elim URL Frontier
Basic crawl architecture Sec. 20.2.1 Basic crawl architecture WWW DNS Dup URL elim set Content seen? Doc FP’s URL filter robots filters Parse Fetch URL Frontier
URL frontier Can include multiple pages from the same host Sec. 20.2 URL frontier Can include multiple pages from the same host Must avoid trying to fetch them all at the same time Must try to keep all crawling threads busy
Basic crawl architecture Sec. 20.2.1 Basic crawl architecture WWW DNS Dup URL elim set Content seen? Doc FP’s URL filter robots filters Parse Fetch URL Frontier
DNS (Domain Name Server) Sec. 20.2.2 DNS (Domain Name Server) A lookup service on the internet Given a URL, retrieve its IP address Service provided by a distributed set of servers – thus, lookup latencies can be high (even seconds) Common OS implementations of DNS lookup are blocking: only one outstanding request at a time Solutions DNS caching Batch DNS resolver – collects requests and sends them out together
Basic crawl architecture Sec. 20.2.1 Basic crawl architecture WWW DNS Dup URL elim set Content seen? Doc FP’s URL filter robots filters Parse Fetch URL Frontier
Content seen? Duplication is widespread on the web Sec. 20.2.1 Content seen? Duplication is widespread on the web If the page just fetched is already in the index, do not further process it This is verified using document fingerprints or shingles Second part of this lecture
Basic crawl architecture Sec. 20.2.1 Basic crawl architecture WWW DNS Dup URL elim set Content seen? Doc FP’s URL filter robots filters Parse Fetch URL Frontier
Explicit and implicit politeness Sec. 20.2 Explicit and implicit politeness Explicit politeness: specifications from webmasters on what portions of site can be crawled robots.txt Implicit politeness: even with no specification, avoid hitting any site too often
Sec. 20.2.1 Robots.txt Protocol for giving spiders (“robots”) limited access to a website, originally from 1994 www.robotstxt.org/wc/norobots.html Website announces its request on what can(not) be crawled For a server, create a file /robots.txt This file specifies access restrictions Crawlers are expected to follow these instructions
Robots Exclusion Protocol Put the file robots.txt at the root directory of the server http://www.cnn.com/robots.txt http://www.w3.org/robots.txt http://www.ynet.co.il/robots.txt http://www.google.com/robots.txt http://www.cs.huji.ac.il/robots.txt
A robots.txt file consists of several records robots.txt Format A robots.txt file consists of several records Each record consists of a set of some crawler id’s and a set of URLs these crawlers are not allowed to visit User-agent lines: Names of crawlers Disallowed lines: Which URLs are not to be visited by these crawlers (agents)?
Examples User-agent: * User-agent: * Disallow: /cgi-bin/ Disallow: / Disallow: /tmp/ Disallow: /junk/ User-agent: * Disallow: / User-agent: * Disallow: User-agent: BadBot Disallow: / User-agent: Google Disallow: User-agent: * Disallow: /
Robots Meta Tag A Web-page author can also publish directions for crawlers These are expressed by the meta tag with name robots, inside the HTML file Format: <meta name="robots" content="options"/> Options: index or noindex: index or do not index this file follow or nofollow: follow or do not follow the links of this file
How should a crawler act when it visits this page? Robots Meta Tag An Example: <html> <head> <meta name="robots" content="noindex,follow"> <title>...</title> </head> <body> … How should a crawler act when it visits this page?
Revisit Meta Tag Web page authors may want Web applications to have an up-to-date copy of their page Using the revisit meta tag, page authors can give crawlers some idea of how often the page is being updated For example: <meta name="revisit-after" content="10 days" />
Stronger Restrictions It is possible for a (non-polite) crawler to ignore the restrictions imposed by robots.txt and robots meta directions Therefore, if one wants to ensure that automatic robots do not visit her resources, she has to use other mechanisms For example, password protections
Interesting Questions Can you be sued for copyright infringement if you don’t follow the robots.txt protocol? Can you be sued for copyright infringement if you follow the robots.txt protocol? Can the site owner determine who is actually crawling their site? Can the site owner protect against unauthorized access by specific robots?
Basic crawl architecture Sec. 20.2.1 Basic crawl architecture WWW DNS Dup URL elim set Content seen? Doc FP’s URL filter robots filters Parse Fetch URL Frontier
Parsing: URL normalization Sec. 20.2.1 Parsing: URL normalization When a fetched document is parsed, some of the extracted links are relative URLs E.g., http://en.wikipedia.org/wiki/Main_Page has a relative link to /wiki/Wikipedia:General_disclaimer which is the same as the absolute URL http://en.wikipedia.org/wiki/Wikipedia:General_disclaimer During parsing, must normalize (expand) such relative URLs
Duplicate URL elimination Sec. 20.2.1 Duplicate URL elimination For a non-continuous (one-shot) crawl, test to see if an extracted+filtered URL has already been passed to the frontier For a continuous crawl – see details of frontier implementation
Distributing the crawler Sec. 20.2.1 Distributing the crawler Run multiple crawl threads, under different processes – potentially at different nodes Geographically distributed nodes Partition hosts being crawled into nodes Hash used for partition How do these nodes communicate and share URLs?
Communication between nodes Sec. 20.2.1 Communication between nodes Output of the URL filter at each node is sent to the Dup URL Eliminator of the appropriate node WWW DNS To other nodes URL set Doc FP’s robots filters Parse Fetch Content seen? URL filter Host splitter Dup URL elim From other nodes URL Frontier
URL frontier: two main considerations Sec. 20.2.3 URL frontier: two main considerations Politeness: do not hit a web server too frequently Freshness: crawl some pages more often than others E.g., pages (such as News sites) whose content changes often These goals may conflict each other. (E.g., simple priority queue fails – many links out of a page go to its own site, creating a burst of accesses to that site.)
Politeness – challenges Sec. 20.2.3 Politeness – challenges Even if we restrict only one thread to fetch from a host, can hit it repeatedly Common heuristic: insert time gap between successive requests to a host that is >> time for most recent fetch from that host
URL frontier: Mercator scheme Sec. 20.2.3 URL frontier: Mercator scheme Prioritizer K front queues URLs Biased front queue selector Back queue router Back queue selector B back queues Single host on each Crawl thread requesting URL
Mercator URL frontier URLs flow in from the top into the frontier Sec. 20.2.3 Mercator URL frontier URLs flow in from the top into the frontier Front queues manage prioritization Back queues enforce politeness Each queue is FIFO
Biased front queue selector Sec. 20.2.3 Front queues Prioritizer 1 K Biased front queue selector Back queue router
Sec. 20.2.3 Front queues Prioritizer assigns to URL an integer priority between 1 and K Appends URL to corresponding queue Heuristics for assigning priority Refresh rate sampled from previous crawls Application-specific (e.g., “crawl news sites more often”)
Biased front queue selector Sec. 20.2.3 Biased front queue selector When a back queue requests a URL (in a sequence to be described): picks a front queue from which to pull a URL This choice can be round robin biased to queues of higher priority, or some more sophisticated variant Can be randomized
Biased front queue selector Sec. 20.2.3 Back queues Biased front queue selector Back queue router 1 B Heap Back queue selector
Sec. 20.2.3 Back queue invariants Each back queue is kept non-empty while the crawl is in progress Each back queue only contains URLs from a single host Maintain a table from hosts to back queues Host name Back queue … 3 1 B
Back queue heap One entry for each back queue Sec. 20.2.3 Back queue heap One entry for each back queue The entry is the earliest time at which the host corresponding to the back queue can be hit again This earliest time is determined from Last access to that host Any time buffer heuristic we choose
Back queue processing A crawler thread seeking a URL to crawl: Sec. 20.2.3 Back queue processing A crawler thread seeking a URL to crawl: Extracts the root of the heap Fetches URL at head of corresponding back queue q (look up from table) Checks if queue q is now empty – if so, pulls a URL v from front queues If there’s already a back queue for v’s host, append v to q and pull another URL from front queues, repeat Else add v to q When q is non-empty, create heap entry for it
Sec. 20.2.3 Number of back queues B Keep all threads busy while respecting politeness Mercator recommendation: three times as many back queues as crawler threads
Near Duplicate Document Detection FP’s Content seen?
Duplicate documents The web is full of duplicated content Sec. 19.6 Duplicate documents The web is full of duplicated content Strict duplicate detection = exact match Not as common But many, many cases of near duplicates E.g., Last modified date the only difference between two copies of a page
Duplicate/Near-Duplicate Detection Sec. 19.6 Duplicate/Near-Duplicate Detection Duplication: Exact match can be detected with fingerprints (= Hash of content) Near-Duplication: Approximate match Compute syntactic similarity with an edit-distance measure Use similarity threshold to detect near-duplicates E.g., Similarity > 80% => Documents are “near duplicates” Not transitive though sometimes used transitively Too Expensive!!
Detecting approximate duplicates Create a sketch for each page that is much smaller than the page Extract a set of features for each page Compare pages by comparing sets of features In the following, assume: we have converted page into a sequence of tokens Eliminate punctuation, HTML markup, etc
Shingling Given document D, a w-shingle is a contiguous subsequence of w tokens The w-shingling S(D,w) of D, is the set of all w-shingles in D Example: D=(a rose is a rose is a rose) a rose is a rose is a rose is a rose is S(D,4) = {(a,rose,is,a),(rose,is,a,rose), (is,a,rose,is)}
Resemblance The resemblance of docs A and B is defined as: Remember this? The resemblance of docs A and B is defined as: In general, 0 rw(A,B) 1 Note rw(A,A) = 1 But rw(A,B)=1 does not mean A and B are identical! (What is a good value for w?)
Sketches Set of all shingles is large Bigger than the original document We cannot compute resemblance! We create a document sketch by sampling only a few shingles Requirement Sketch resemblance should be a good estimate of document resemblance Notation: From now on, w will be a fixed value, so we omit w from rw and S(A,w) r(A,B)=rw(A,B) and S(A) = S(A,w)
Comparing Documents Basic structure of document comparison process Note that sketches of documents that have already been seen are stored by crawler Doc A Shingle set A Sketch A Doc B Shingle set B Sketch B Jaccard
Choosing a sketch Random sampling does not work! Suppose we have identical documents A, B each with n shingles Let mA be a shingle from A, chosen uniformly at random; similarly mB For k=1: E[{mA} {mB}|] = 1/n But r(A,B) = 1 So the sketch overlap is an underestimate
Choosing a sketch Assume the set of all possible shingles is totally ordered Define: mA is the “smallest” shingle in A mB is the “smallest” shingle in B r’(A,B) = | {mA} {mB} | For identical documents A & B r’(A,B) = 1 = r(A,B)
Problems With a sketch of size 1, there is still a high probability of error Especially if we choose minimum shingle, e.g., in alphabetical order Size of each shingle is still large e.g., if w = 7, about 40-50 bytes 200 shingles = 8K-10K
Sketch of a document For doc D, sketchD[ i ] is as follows: Sec. 19.6 Sketch of a document Create a “sketch vector” (of size ~200) for each document Documents that share ≥ t (say 80%) corresponding vector elements are deemed near duplicates For doc D, sketchD[ i ] is as follows: Let f map all shingles in the universe to 0..264 (e.g., f = fingerprinting) Let i be a random permutation on 0..264 sketchD[i] = min {(f(s)) | s shingle in D}
Computing Sketch[i] for Doc1 Sec. 19.6 Computing Sketch[i] for Doc1 Document 1 Compute shingles of Doc 1 Take fingerprints f(shingles) Permute on the number line with pi Pick the min value 264 264 264 264 Repeat 200 times!
Test if Doc1.Sketch[i] = Doc2.Sketch[i] Sec. 19.6 Test if Doc1.Sketch[i] = Doc2.Sketch[i] Document 2 Document 1 264 264 264 264 264 264 A B 264 264 Are these equal? Test for 200 random permutations: p1, p2,… p200
However… Document 1 Document 2 264 Sec. 19.6 Document 1 Document 2 264 A B A = B iff the shingle with the MIN value in the union of Doc1 and Doc2 is common to both (i.e., lies in the intersection) Claim: This happens with probability Size_of_intersection / Size_of_union Why?
Set Similarity of sets Ci , Cj Sec. 19.6 Set Similarity of sets Ci , Cj View sets as columns of a matrix A; one row for each element in the universe. aij = 1 indicates presence of item i in set j Example C1 C2 0 1 1 0 1 1 Jaccard(C1,C2) = 2/5 = 0.4 0 0 1 1 0 1
Key Observation Ci Cj For columns Ci, Cj, four types of rows Sec. 19.6 Key Observation For columns Ci, Cj, four types of rows Ci Cj A 1 1 B 1 0 C 0 1 D 0 0 Overload notation: A = # of rows of type A Claim
“Min” Hashing Randomly permute rows Sec. 19.6 “Min” Hashing Randomly permute rows Hash h(Ci) = index of first row with 1 in column Ci Surprising Property Why? Both are A/(A+B+C) Look down columns Ci, Cj until first non-Type-D row h(Ci) = h(Cj) iff type A row
Similar Documents Remember: We test with 200 random permutations So, probability that D1 and D2 are declared similar is Jaccard(S(D1),S(D2))200
Computing Random Permutations Compute 200 different fingerprint(s) for each shingle For example, use MD5 hashing function over shingle%i, where 1<=i<=200 For each fingerprint function, choose lexicographically first shingle in each document Near duplication likelihood is computed by checking how many of the 200 smallest shingles are common to both documents
Solution (cont) Observe that this gives us: An efficient way to compute random orders of shingles Significant reduction in size of shingles stored (40 bits is usually enough to keep estimates reasonably accurate)