【问题标题】:parsing amazon page using jsoup returns 204 status使用 jsoup 解析亚马逊页面返回 204 状态
【发布时间】:2013-04-05 20:15:26
【问题描述】:

示例页面:http://www.amazon.com/gp/offer-listing/1589942140

public void connect( String url ) {        
    this.conn = Jsoup.connect( url );  
}

/**
 * Executes the request and parses the result.
 * @return 
 */
public boolean parse() 
{
    try {
        this.page = this.conn.get();
        return true;
    } catch (IOException ex) {
        // log it here
        System.out.format("Error: %s%n", ex);
        return false;
    }
}    

解析页面会在下面创建ioexception:

org.jsoup.HttpStatusException:获取 URL 的 HTTP 错误。状态=204,网址=http://www.amazon.com/gp/offer-listing/1589942140

我用下面的本地 java url 类尝试过,它没有创建 IOException:

    try {
        URL myURL = new URL("http://www.amazon.com/gp/offer-listing/1589942140");
        URLConnection myURLConnection = myURL.openConnection();
        myURLConnection.connect();
        System.out.format("%s", myURLConnection.getContentType());
    } 
    catch (MalformedURLException e) { 
        // new URL() failed
        System.out.format("Error: %s%n", e);
    } 
    catch (IOException e) {   
        // openConnection() failed
        System.out.format("Error: %s%n", e);
    }

任何想法为什么会这样?

【问题讨论】:

    标签: java html parsing jsoup


    【解决方案1】:

    以下对我有用:

                System.out.println(Jsoup.connect("http://www.amazon.com/gp/offer-listing/1589942140").userAgent("Mozilla").get().text());;
    

    上面尝试的 URL 是您在上面指定的。 (示例页面:http://www.amazon.com/gp/offer-listing/1589942140

    【讨论】:

    • 是的,我在使用 Connection 类中的其他方法时自己找到了解决方案。谢谢发帖,寿。我会让这个正确答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多