csci572 Homework Web Crawling
1. Objective
In this assignment, you will work with a simple web crawler to measure aspects of a crawl, study the characteristics of the crawl, download web pages from the crawl and gather webpage metadata, all from pre-selected news websites.
2. Preliminaries
Copyright By PowCoder代写 加微信 powcoder
To begin we will make use of an existing open source Java web crawler called crawler4j. This crawler is built upon the open source crawler4j library which is located on github. For complete details on downloading and compiling see
Also see the document “Instructions for Installing Eclipse and Crawler4j” located on the Assignments web page for help.
Note: You can use any IDE of your choice. But we have provided installation instructions for Eclipse IDE only
3. Crawling
Your task is to configure and compile the crawler and then have it crawl a news website. In the interest of distributing the load evenly and not overloading the news servers, we have pre-assigned the news sites to be crawled according to your USC ID number, given in the table below.
The maximum pages to fetch can be set in crawler4j and it should be set to 20,000 to ensure a reasonable execution time for this exercise. Also, maximum depth should be set to 16 to ensure that we limit the crawling.
You should crawl only the news websites assigned to you, and your crawler should be configured so that it does not visit pages outside of the given news website!
USC ID ends with
News Sites to Crawl
Ne wsS ite Na me
01~20 NY Times
21~40 Wall Street Journal 41~60 Fox News
61~80 USA Today
81~00 Los Angeles Times
nytimes wsj foxnews usatoday latimes
https://www.nytimes.com https://www.wsj.com https://www.foxnews.com https://www.usatoday.com https://www.latimes.com
Limit your crawler so it only visits HTML, doc, pdf and different image format URLs and record the meta data for those file types
4. Collecting Statistics
Your primary task is to enhance the crawler so it collects information about:
1. the URLs it attempts to fetch, a two column spreadsheet, column 1 containing the URL and
column 2 containing the HTTP/HTTPS status code received; name the file fetch_NewsSite.csv (where the name “NewsSite” is replaced by the news website name in the table above that you are crawling). The number of rows should be no more than 20,000 as that is our pre-set limit. Column names for this file can be URL and Status
2. the files it successfully downloads, a four column spreadsheet, column 1 containing the URLs successfully downloaded, column 2 containing the size of the downloaded file (in Bytes, or you can choose your own preferred unit (bytes,kb,mb)), column 3 containing the # of outlinks found, and column 4 containing the resulting content-type; name the file visit_NewsSite.csv; clearly the number of rows will be less than the number of rows in fetch_NewsSite.csv
3. all of the URLs (including repeats) that were discovered and processed in some way; a two column spreadsheet where column 1 contains the encountered URL and column two an indicator of whether the URL a. resides in the website (OK), or b. points outside of the website (N_OK). (A file points out of the website if its URL does not start with the initial host/domain name, e.g. when crawling USA Today news website all inside URLs must start with
.) Name the file urls_NewsSite.csv. This file will be much larger than fetch_*.csv and visit_*.csv.
For example for Times- the URL and the URL are both considered as residing in the same website whereas the following URL is not considered to be in the same website, http://store.nytimes.com/
Note1: you should modify the crawler so it outputs the above data into three separate csv files; you will use them for processing later;
Note2: all uses of NewsSite above should be replaced by the name given in the column labeled NewsSite Name in the table on page 1.
Note 3: You should denote the units in size column of visit.csv. The best way would be to write the units that you are using in column header name and let the rest of the size data be in numbers for easier statistical analysis. The hard requirement is only to show the units clearly and correctly.
Based on the information recorded by the crawler in the output files above, you are to collate the following statistics for a crawl of your designated news website:
● Fetch statistics:
o #fetchesattempted:
The total number of URLs that the crawler attempted to fetch. This is usually equal to the MAXPAGES setting if the crawler reached that limit; less if the website is smaller than that.
o #fetchessucceeded:
The number of URLs that were successfully downloaded in their entirety, i.e. returning a HTTP status code of 2XX.
o #fetchesfailedoraborted:
The number of fetches that failed for whatever reason, including, but not limited to: HTTP
redirections (3XX), client errors (4XX), server errors (5XX) and other network-related errors.1
● Outgoing URLs: statistics about URLs extracted from visited HTML pages o Total URLs extracted:
The grand total number of URLs extracted (including repeats) from all visited pages o # unique URLs extracted:
The number of unique URLs encountered by the crawler
o # unique URLs within your news website:
The number of unique URLs encountered that are associated with the news website,
i.e. the URL begins with the given root URL of the news website, but the remainder of the URL is distinct
o # unique URLs outside the news website:
The number of unique URLs encountered that were not from the news website.
● Status codes: number of times various HTTP status codes were encountered during crawling, including (but not limited to): 200, 301, 401, 402, 404, etc.
● File sizes: statistics about file sizes of visited URLs – the number of files in each size range (See Appendix A).
o 1KB = 1024B; 1MB = 1024KB
● Content Type: a list of the different content-types encountered
These statistics should be collated and submitted as a plain text file whose name is CrawlReport_NewsSite.txt, following the format given in Appendix A at the end of this document. Make sure you understand the crawler code and required output before you commence collating these statistics.
For efficient crawling it is a good idea to have multiple crawling threads. You are required to use multiple threads in this exercise. crawler4j supports multi-threading and our examples show setting the number of crawlers to seven (see the line in the code int numberOfCrawlers = 7;). However, if you do a naive implementation the threads will trample on each other when outputting to your statistics collection files. Therefore you need to be a bit smarter about how to collect the statistics, and crawler4j documentation has a good example of how to do this. See both of the following links for details:
https://github.com/yasserg/crawler4j/blob/master/crawler4j-examples/crawler4j-examples- base/src/test/java/edu/uci/ics/crawler4j/examples/localdata/LocalDataCollectorCrawler.java
All the information that you are required to collect can be derived by processing the crawler output.
Q: For the purposes of counting unique URLs, how to handle URLs that differ only in the query string? For example: https://www.nytimes.com/page?q=0 and https://www.nytimes.com/page?q=1
1 Based purely on the success/failure of the fetching process. Do not include errors caused by difficulty in parsing content after it has already been successfully downloaded.
A: These can be treated as different URLs.
Q: URL case sensitivity: are these the same, or different URLs?
https://www.nytimes.com/foo and https://www.nytimes.com/FOO A: The path component of a URL is considered to be case-sensitive, so the crawler behavior is
correct according to RFC3986. Therefore, these are different URLs.
The page served may be the same because:
● that particular web server implementation treats path as case-insensitive (some server
implementations do this, especially windows-based implementations)
● the web server implementation treats path as case-sensitive, but aliasing or redirect is being
This is one of the reasons why deduplication is necessary in practice.
Q: Attempting to compile the crawler results in syntax errors.
A: Make sure that you have included crawler4j as well as all its dependencies.
Also check your Java version; the code includes more recent Java constructs such as the typed collection List
Q: I get the following warnings when trying to run the crawler:
A: You failed to include the log4j.properties file that comes with crawler4j. Q: On Windows, I am encountering the error: Exception_Access_Violation
A: This is a Java issue. See:
Q: I am encountering multiple instances of this info message:
A: If you’re working off an unsteady wireless link, you may be battling network issues such as packet losses – try to use a better connection. If not, the web server may be struggling to keep up with the frequency of your requests.
As indicated by the info message, the crawler will retry the fetch, so a few isolated occurrences of this message are not an issue. However, if the problem repeats persistently, the situation is not likely to improve if you continue hammering the server at the same frequency. Try giving the server more room to breathe:
log4j: WARN No appenders could be found for logger log4j: WARN Please initialize the log4j system properly.
INFO [Crawler 1] I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond INFO [Crawler 1] Retrying request
* Be polite: Make sure that we don’t send more than
* 1 request per second (1000 milliseconds between requests). */
config.setPolitenessDelay(2500);
* READ ROBOTS.TXT of the website – Crawl-Delay: 10
* Multiply that value by 1000 for millisecond value */
Q: The crawler seems to choke on some of the downloaded files, for example: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
java.lang.NullPointerException: charsetName
A: Safelyignorethose.Weareusingafairlysimple,rudimentarycrawleranditisnotnecessarily robust enough to handle all the possible quirks of heavy-duty crawling and parsing. These problems are few in number (compared to the entire crawl size), and for this exercise we’re okay with it as long as it skips the few problem cases and keeps crawling everything else, and terminates properly – as opposed to exiting with fatal errors.
Q: While running the crawler, you may get the following error: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See
for further details.
A. Download slf4j-simple-1.7.25.jar from
add this as an external JAR to the project in the same way as the crawler-4j JAR
will make the crawler display logs now.
Q: What should we do with URL if it contains comma ?
A: Replace the comma with “-” or “_”, so that it doesn’t throw an error.
Q: Should the number of 200 codes in the fetch.csv file have to exactly match with the number of records in the visit.csv?
A: No, but it should be close, like within 2,000 of 20,000. If not then you may be filtering too much.
Q: “CrawlConfig cannot be resolved to a type” ?
A: import edu.uci.ics.crawler4j.crawler.CrawlConfig; make sure the external jars are added to ClassPath. ModulePath only contains the JRE. If it doesn’t work, check if standard JRE imports are working. Or using an alternative way: Using maven. Initialize a new project using maven and
then just add a crawler4j dependency in the pom.xml file (auto-generated by maven). The dependency is given in Crawler4j github page.
Q: What’s the difference between aborted fetches and failed fetches? A: failed: Can be due to HTTP errors and other network related errors
aborted: Client decided to stop the fetching. (ex: Taking too much time to fetch) You may sum up both the values and provide the combined result in the write up.
Q: For some reason my crawler attempts 19,999 fetches, even though max pages is set to 20,000, does this matter?
A: No, it doesn’t matter. It can occur because 20,000 is the limit that you will try to fetch (it may contain successful status code like 200 and other like 301). But the visit.csv will contain only the URL’s for which you are able to successfully download the files.
Q: How to differentiate fetched pages and downloaded pages?
A: In this assignment we do not ask you to save any of the downloaded files to the disk. Visiting a page means crawler4j processing a page (it will parse the page and extract relevant information like outgoing URLs ). That means all visited pages are downloaded.
You must make sure that your crawler crawls both http and https pages of the given domain
Q: How much time should it approximately take to crawl a website using n crawlers? A: (i) Depends on your parameters set for the crawler
(ii) Depends on the politeness you set in the crawler program
Your crawl time in hours = maxPagesToFetch / 3600 * politeness delay in seconds
Example: a 20,000 page fetch with a politeness delay of 2 seconds will take 11.11 hours. That is assuming you are running enough threads to ensure a page fetch every 2 seconds. Therefore, it can vary for everyone.
Q: For the third CSV file, urls_NewSite.csv, should the discovered URLs include redirect URLs?
A: YES, if the redirect URL is the one that gets status code 300, then the URL that redirects the URL to point to will be added to the scheduler of the crawler and waits to be visited.
Q: Eclipse keeps crashing after a few minutes of running my code. But when I reduce the no of pages to fetch, it works fine.
A: Increase heap size for eclipse using this.
Q: What if a URL has an unknown extension?
A: Please check the content type of the page if it has an unknown extension
Q: Why do some links return True in shouldVisit() but cannot be visited by Visit()?
A: shouldVisit() function is used to calculate whether the page should be visited or not. It may or may not be a visitable page.
When the URL ends with “/”, what needs to be done?
A: You should filter using content type. Please have a peek into Crawler 4j code located at
You will get a hint on how to know the content type of the page, even if the extension is not explicitly mentioned in the URL
For example – If you are crawling the site http://viterbi.usc.edu/, the page http://viterbi.usc.edu/mySamplePage.html should be visited. but this page may return a 404 Not Found Error or it may be redirected to some other site like http://mysamplesite.com. In this case, shouldVisit() function would return true because the page should be visited but visit() will not be called because the page cannot be visited.
has details on regular expressions that you need to take care.
Comment: Since many newspaper websites dump images and other types of media on CDN, your crawl may only encounter html files. That is fine.
Comment: File types css,js,json and others should not be visited. E.g. you can add .json to your pattern filter. If the extension does not appear, use
!page.getContentType().contains(“application.json”)
Comment: Some sites may have less than the 20,000 pages, but as long as the formula matches. i,e
# fetches attempted = # fetches succeeded + # fetches aborted + # fetches failed
your homework is ok. However, the variation should not be more than 10% away from the limit as it is an indication that something is wrong.
My visit.csv file has about 15 URLs lesser than the number of URLs with status code 200. It is fine if the difference is less than 10%.
Comment: the homework description states that you only need to consider HTML, doc, pdf and different image format URLs . But you should also consider URL’s with no extension as they may return a file of one of the above types.
Comment: The distinction between failed and aborted web pages.
failed: Can be due to content not found, HTTP errors or other network related errors
aborted: the client (the crawler) decided to stop the fetching. (ex: Taking too much time to fetch).
You may sum up both the values and provide the combined result in the write up.
Q: In the visit_NewsSite.csv, do we also need to chop “charset=utf-8” from content-type? Or just chop “charset=urf-8” in the report?
A: You can chop Encoding part(charset=urf-8) in all places.
Q: REGARDING STATISTICS
A: #unique URLs extracted = #unique URLs within + #unique URLs outside
#total urls extracted is the sum of #outgoing links.
#total urls extracted is the sum of all values in column 3 of visit.csv
For text/html files, find the number of out links. For non-text/html files, the number should be 0.
Q: How to handle pages with NO Extension
A: Use getContentType() in visit() and don’t rely just on extension. If the content type returned is not one of the required content types for the assignment, you should ignore it for any calculation of the statistics. This will probably result in more rows in visit.csv, but it’s acceptable according to the grading guidelines.
Q: Clarification on “the URLs it attempts to fetch”
A: “The URLs it attempts to fetch” means all the URLs crawled from start seed which reside in the news website and has the required media types.
Note #1: Extracted urls do not have to be added to visit queue. Some of them which satisfy a requirement (e.g : content type, domain, not duplicate) will be added to visit queue. But others will be dumped by the crawler.
However, as long as the grading guideline is satisfied, we will not deduct points.
Note#2: : 303 could be considered aborted. 404 could be considered failed.
To summarize: we consider a request to be aborted if the crawler decides to terminate that
request. Client-side timeout is an example. Requests can fail due to reasons like content not found, server errors, etc.
Note#3: Fetch statistics:
# fetches attempted: The total number of URLs that the crawler attempted to fetch. This is
usually equal to the MAXPAGES setting if the crawler reached that limit; less if the website is smaller than that.
# fetches succeeded: The number of URLs that were successfully downloaded in their entirety, i.e. returning a HTTP status code of 2XX.
# fetches failed or aborted: The number of fetches that failed for whatever reason, including, but not limited to: HTTP redirections (3XX), client errors (4XX), server errors (5XX) and other network-related errors.
Note#4: Consider fetches failed and aborted as same similar to as mentioned in Note#3
Note#5: Hint on crawling pages other than html
Look for how to turn ON the Binary Content in Crawling in crawler4j. Make sure you are not just crawling the html parsed data and not the binary data which includes file types other than html.
Search on the internet on how to crawl binary data and I am sure yo
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com