scrape ASIN from amazon URL using javascript

前端 未结 16 744
旧巷少年郎
旧巷少年郎 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:14

    @Gumbo: Your code works great!

    //JS Test: Test it into firebug.

    url = window.location.href;
    url.match("/([a-zA-Z0-9]{10})(?:[/?]|$)");
    

    I add a php function that makes the same thing.

    function amazon_get_asin_code($url) {
        global $debug;
    
        $result = "";
    
        $pattern = "([a-zA-Z0-9]{10})(?:[/?]|$)";
        $pattern = escapeshellarg($pattern);
    
        preg_match($pattern, $url, $matches);
    
        if($debug) {
            var_dump($matches);
        }
    
        if($matches && isset($matches[1])) {
            $result = $matches[1];
        } 
    
        return $result;
    }
    

提交回复
热议问题