0

Is there a way to find out the fully qualified url in Nodejs?

Basically I want to crawl a website using my custom node.js based crawler. I fetch all the anchor tags and make http requests on them. The problem is that I am not able to figure out the exact url that I need to make the request to. Please help

Example URLS

http://aaa.com/bbb

//aaa.com/bbb or //aaa/bbb

/aaa.com/bbb or /aaa/bbb

aaa.com/bbb or aaa/bbb

4
  • have you heard about url.join()? Commented Feb 3, 2014 at 10:51
  • But the urls are specified in different formats every where. A simple join will not solve the problem. I need an algo in place to do that. Commented Feb 3, 2014 at 10:54
  • 1
    url.join() + url.resolve() can do the magic, just provide some examples where you can not use them Commented Feb 3, 2014 at 10:57
  • @micnic I added some sample urls that I would like to get the fully qualified urls of. Commented Feb 3, 2014 at 11:03

2 Answers 2

1

The url.resolve feature worked for me (suggested by @Karman and @micnic). The first argument was the base url, which represented the url of the page that I was scraping for anchor tags. The second argument is the url which is saved in the href tag of the anchors.

Sign up to request clarification or add additional context in comments.

Comments

0

Look at the url module http://nodejs.org/api/url.html you can use url resolve property or have detail look at the above mentioned documentation of url module

also e.g.

var obj=  require('url').parse('http://google.com?q=tobi');
will result in
obj= 
{ protocol: 'http:',
slashes: true,
host: 'google.com',
hostname: 'google.com',
href: 'http://google.com/?q=tobi',
search: '?q=tobi',
query: 'q=tobi',
pathname: '/' }

2 Comments

This would fail for 'aaa/bbbb'. Even though its a valid url on the page.
Good. Share your code about how u resolve it so it might also helps others. Also accpet the post as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.