scrape ASIN from amazon URL using javascript

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

    This may be a simplistic approach, but I have yet to find an error in it using any of the URL's provided in this thread that people say is an issue.

    Simply, I take the URL, split it on the "/" to get the discrete parts. Then loop through the contents of the array and bounce them off of the regex. In my case the variable i represents an object that has a property called RawURL to contain the raw url that I am working with and a property called VendorSKU that I am populating.

    try
                {
                    string[] urlParts = i.RawURL.Split('/');
                    Regex regex = new Regex(@"^[A-Z0-9]{10}");
    
                    foreach (string part in urlParts)
                    {
                        Match m = regex.Match(part);
                        if (m.Success)
                        {
                            i.VendorSKU = m.Value;
                        }
                    }
                }
                catch (Exception) { }
    

    So far, this has worked perfectly.

提交回复
热议问题