【问题标题】:Discover each product url [closed]发现每个产品网址 [关闭]
【发布时间】:2021-08-05 22:09:35
【问题描述】:

https://i.stack.imgur.com/zFqm1.png

需要提取所有产品网址,试过这个。大部分都买不到

url = "https://alcohaul.sg/"

var x = document.querySelectorAll("a");
var myarray = []
for (var i=0; i<x.length; i++){
var nametext = x[i].textContent;
var cleantext = nametext.replace(/\s+/g, ' ').trim();
var cleanlink = x[i].href;
myarray.push([cleantext,cleanlink]);
};
function make_table() {
    var table = '<table><thead><th>Name</th><th>Links</th></thead><tbody>';
   for (var i=0; i<myarray.length; i++) {
            table += '<tr><td>'+ myarray[i][0] + '</td><td>'+myarray[i][1]+'</td></tr>';
    };
 
    var w = window.open("");
w.document.write(table); 
}
make_table()

【问题讨论】:

  • 滚动页面时产品是否动态加载?

标签: javascript python web-scraping beautifulsoup scrapy


【解决方案1】:

当您说“所有产品 URL”时,您指的是在主页上可见的产品,是吗?

我在浏览器中访问了该页面并记录了我的网络流量。浏览器向某些 REST API 发出各种 HTTP GET 请求。其中一个具有端点api/newarrival,其响应包含主页上产品的所有产品信息 - 以 JSON 格式。您所要做的就是模仿那个 HTTP GET 请求。这个 API 看起来很宽松——不需要请求标头,甚至不需要任何查询字符串参数。但是,这也意味着您可以预期此 API 的响应会随着新的“新来者”的到来而随时间变化。

def main():

    import requests

    url = "https://alcohaul.sg/api/newarrival"

    response = requests.get(url)
    response.raise_for_status()

    base_url = "https://alcohaul.sg/products/{}"

    for product in response.json():
        print(base_url.format(product["slug"]))

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())

输出:

https://alcohaul.sg/products/absolut-vodka-blue-70cl
https://alcohaul.sg/products/campari-70cl
https://alcohaul.sg/products/botanist-gin-70cl
https://alcohaul.sg/products/strangelove-premium-light-tonic-pack-4-x-180ml
https://alcohaul.sg/products/strangelove-premium-indian-tonic-no-8-pack-4-x-180ml
https://alcohaul.sg/products/strangelove-premium-coastal-tonic-pack-4-x-180ml
https://alcohaul.sg/products/strangelove-premium-lo-cal-double-ginger-beer-pack-4-x-300ml
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多