【问题标题】:Why I cant get all page为什么我无法获取所有页面
【发布时间】:2019-07-24 12:51:49
【问题描述】:

在页面https://www.jogossantacasa.pt/web/Placard/placard,我正在尝试获取Futebol->... 的链接。我可以,但这只会在for 循环上刮掉一页。谢谢大家。

public class main {

    static List<String> links=new ArrayList<>();
    static List<String> ligas=new ArrayList<>();
    static String url="https://www.jogossantacasa.pt"; //main link

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Document doc;
        // Here i get the links
        try {
            doc = Jsoup.connect(url+"/web/Placard/placard").get();
            Elements a = doc.getElementsByClass("width9");
            boolean qwerty = true;
            for(Element ele : a) {
                Elements k = ele.select("li");      
                for(Element d : k)
                {   
                    String hj = d.select("a").text();
            
                    if(hj.contains("Ténis")) qwerty = false;
                    if(qwerty) {
                        if(!hj.contains("Futebol")) {
                            links.add(d.select("a").attr("href"));
                            ligas.add(hj);
                        }
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Here I try to scrape each country page and error is only the last page is scraped
        for(int i = 0 ; i < links.size() ; i++) {

            String urlEach=url+links.get(i);
            Document docEach;

            try {
                docEach = Jsoup.connect(urlEach).get();
                System.out.println(docEach.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        }
    }
}

【问题讨论】:

  • 哦,拜托,boolean qwerty?
  • @KrystianG 我不明白你的意思。
  • 对变量使用描述性名称。 boolean qwerty 看起来很糟糕,很难阅读并且没有任何意义。你可以把它命名为boolean notTennis。

标签: java web-scraping jsoup


【解决方案1】:

首页 (/web/Placard/eventos?id=23316) 很大,超过 3MB。 Jsoup 仅下载此文件的前 1MB。要克服此限制,请在连接时设置更高的maxBodySize 或0 以禁用限制。

docEach = Jsoup.connect(urlEach).maxBodySize(10*1024*1024).get(); // 10MB

【讨论】:

    猜你喜欢
    • 2022-06-23
    • 1970-01-01
    • 2011-01-25
    • 2021-08-26
    • 2020-05-05
    • 2021-03-21
    • 2018-05-28
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多