Skip to content

Better handling for scraping links, i.e. relative urls and whitespace. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions crawling-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function checkUrl()
}
var lnk = $(this).attr("href").replace(new RegExp("#(.*)"), "");

if ((lnk = check_link(lnk)) == false) {
if ((lnk = check_link(lnk, reqUrl)) == false) {
processingDOM = (--links_found > 0);
return;
}
Expand Down Expand Up @@ -292,17 +292,47 @@ function checkUrl()
});
};


function check_link(lnk)
{
if( lnk.indexOf("/")==0 )
lnk = "http://" + scrapeHost + lnk;

if( lnk==undefined || ["#", ""].indexOf(lnk)!=-1 || (lnk.indexOf("http://" + scrapeHost)!=0 && lnk.indexOf("https://"+scrapeHost)!=0) ) {
return false;
}

return lnk;
/**
* Check a link from a scraped page to make sure it is valid, and then
* normalize it to an absolute url.
*
* @param {String} link The link scraped from the page
* @param {String} parent_link The link of the page that "link" was scraped from
*
* @return {String} The revised link
*/
function check_link(link, parent_link) {
// check for "empty" links
if (link===undefined) {
return false;
} else if (link==='' || link=='#') {
return false;
}
// parse the link
parts = url.parse(link);
// check the scheme
if (parts.protocol=='mailto' || parts.protocol=='javascript' || parts.protocol=='ftp') {
// incompatible protocol
return false;
} else if (parts.protocol=='http' || parts.protocol=='https') {
// make sure host is our ___domain
if (parts.host!=scrapeHost) {
return false;
}
} else if (link.indexOf('//')===0) {
// handle schema-less; ensure host is ours
if (link.indexOf('//' + scrapeHost)!==0) {
return false;
}
link = 'http:' + link;
} else if (parts.protocol) {
// unknown protocol
return false;
} else {
// relative link
link = url.resolve(parent_link, link);
}
return link;
}

function make_request(protocol, host, path, depth, callback)
Expand Down