Jsoup 根据关键字 抓取亚马逊自然排名

  思路就是 根据亚马逊的查询规则组装 请求的ip 然后用jsoup进行模拟请求 获取整个页面最后对整个页面的前端代码进行分析 筛选出来自己想要的.

//核心代码

public void amazonData(Capture capture, List<Productpage> productpageList) {

    Map<String, String> map = new HashMap<String, String>();
    List<String> siteList = OtherUtil.getSite(capture, map);
    for (int i = 0; i < siteList.size(); i++) {


        try {

            Thread.sleep((int) (Math.random() * 5000));

        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        try {
       //siteList.get(i) 获取的是组装好的ip 模拟cookie 模拟谷歌浏览器

            Document document = Jsoup.connect(siteList.get(i)).cookies(map).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36").get();
//根据前端代码里的规律 筛选自己想要的
            Elements elementsByAttribute = document.getElementsByAttribute("data-component-type");
            int num = 1;
            for (int j = 0; j < elementsByAttribute.size(); j++) {

                String str = elementsByAttribute.get(j).toString();
// 这是获取一个页面的全部产品 后续会对照数据库进行排名 
                if (str.startsWith("<div data-asin=") && str.indexOf("data-component-type=\"s-search-result\"") != -1) {

                    Productpage productpage = new Productpage();

                    productpage.setAntistop(capture.getAntistop());

                    productpage.setSite(capture.getSite());

                    productpage.setDivDate(str);

                    productpage.setPageNo((i + 1) + "");

                    productpage.setPm(num + "");

                    productpageList.add(productpage);

                    num++;

                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//英国站的组装  k = "关键字" i为页数 这是只搜索前五页 map里面的是cookie,由于不懂怎么模拟浏览器输入地区编码 用了个最蠢的 写死 后续肯定能够改.
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值