scrape ASIN from amazon URL using javascript

前端 未结 16 719
旧巷少年郎
旧巷少年郎 2021-01-30 11:42

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=ATVP         


        
16条回答
  •  走了就别回头了
    2021-01-30 12:31

    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//dp//ASIN
    http://www.amazon.com/gp/product//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]);
    }
    

提交回复
热议问题