【问题标题】:Why does my JSoup request return an empty document?为什么我的 JSoup 请求返回一个空文档?
【发布时间】:2018-02-13 19:23:42
【问题描述】:

我想抓取一个网站的内容,但它似乎不起作用:

public static void main(String[] args) throws Exception {

        String url = "https://www.rl-trades.com";
        Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36").get();
        System.out.println(doc);
    }

我得到的只有这个:

<html>
 <head></head>
 <body></body>
</html>

问题似乎出在网站上,因为在这里用不同的网站提出的每个类似问题都对我有用。我也试过这个更高级的版本,但我得到了完全相同的结果:

public static void main(String[] args) throws Exception {

        String url = "https://www.rl-trades.com";
        Response response= Jsoup.connect(url)
                .ignoreContentType(true)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")  
                .referrer("http://www.google.com")   
                .timeout(12000) 
                .followRedirects(true)
                .execute();

        Document doc = response.parse();

        System.out.println(doc);
    }

还有什么方法可以获取内容吗?还是只有网站上的刮擦保护而没有解决方法?

提前致谢!

【问题讨论】:

    标签: java web-scraping jsoup screen-scraping user-agent


    【解决方案1】:

    看起来这个网站喜欢 Accept-Language 标头:

    String url = "https://www.rl-trades.com";
    Connection connection = Jsoup.connect(url);
    connection.header("Accept-Language","en");
    Document doc = connection.get();
    System.out.println(doc);
    

    【讨论】:

    • 非常感谢! :) 真的很感激!
    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多