Skip to content
Advertisement

scrape ASIN from amazon URL using javascript

Assuming I have an Amazon product URL like so

http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C/ref=amb_link_86123711_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-1&pf_rd_r=0AY9N5GXRYHCADJP5P0V&pf_rd_t=101&pf_rd_p=500528151&pf_rd_i=507846

How could I scrape just the ASIN using javascript? Thanks!

Advertisement

Answer

Amazon’s detail pages can have several forms, so to be thorough you should check for them all. These are all equivalent:

http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C
http://www.amazon.com/dp/B0015T963C
http://www.amazon.com/gp/product/B0015T963C
http://www.amazon.com/gp/product/glance/B0015T963C

They always look like either this or this:

http://www.amazon.com/<SEO STRING>/dp/<VIEW>/ASIN
http://www.amazon.com/gp/product/<VIEW>/ASIN

This should do it:

var url = "http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C";
var regex = RegExp("http://www.amazon.com/([\w-]+/)?(dp|gp/product)/(\w+/)?(\w{10})");
m = url.match(regex);
if (m) { 
    alert("ASIN=" + m[4]);
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement