java.lang.IndexOutOfBoundsException:索引:0,大小:0错误

时间:2019-12-02 18:21:57

标签: web-scraping jsoup

我正在尝试抓取一个网站以获取产品名称(因为这一直是我的搜索,所以总是相似)和价格,但是当我运行代码时,我总是会遇到相同的错误:

SEVERE: null
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at webscraping.Scraper2.scraper1(Scraper2.java:68)
    at webscraping.Scraper2.run(Scraper2.java:31)

我已经尝试了千种不同的方式来报废它,但是那根本行不通。

我的代码如下:

String website = "Guitar Village";
        //Download HTML document from website
        Document doc = Jsoup.connect("https://guitarvillage.co.uk/products?utf8=%E2%9C%93&keywords=TAYLOR+GS+MINI-E+KOA").get();

        //Get all of the products on the page
        Elements prods = doc.select(".search page");
        Elements prods1 = prods.select("#prod_container_1");
        Elements prods2 = prods1.select(".search-results");
        Elements prods3 = prods2.select(".products.grid");
        Elements prods4 = prods3.select(".listing.grid-style");
        Elements prods5 = prods4.select(".grid");
        Elements prods6 = prods5.select(".col-3");
        Elements prods7 = prods6.select(".product");


        for(int i=0; i<4; ++i){

            //Get the product description
            Elements item = prods5.get(i).select(".details");
            Elements itemName = item.get(i).select(".title");
            Elements price = item.get(i).select(".price");


            //Output the data that we have downloaded
            System.out.println("Website: " + website + " NAME: " + itemName.text() + " PRICE: " + price.text());
        }

有帮助吗?

0 个答案:

没有答案