【问题标题】:Jsoup on imdb 403 error关于imdb 403错误的Jsoup
【发布时间】:2016-07-21 20:42:47
【问题描述】:

我需要解析 imdb 页面才能显示结果。我为此目的使用 Jsoup。下面是我为此目的编写的代码。运行代码时,我看到 403 错误。我重新验证了网址,网址似乎是正确的。

import java.io.IOException;
import java.net.URLEncoder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class ParseIMDB {

    public static void parse() throws IOException{
        Document doc = Jsoup.connect("http://imdb.com/search/title?count=100&genres=action&languages=en&release_date=2010,2016&title_type=feature").get();
        Elements newsHeadlines = doc.select("#main > table.results tbody");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
        parse();
        } catch (Exception e){
            System.out.println("Exception found!");
            e.printStackTrace();
        }
    }
}

我尝试使用 URLEncode.encode 对 url 进行编码,但它也有帮助。

上述代码的堆栈跟踪如下:

发现异常! org.jsoup.HttpStatusException:获取 HTTP 错误 网址。状态=403,URL=http://www.imdb.com/search/title/ 在 org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:537) 在 org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:534) 在 org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493) 在 org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205) 在 org.jsoup.helper.HttpConnection.get(HttpConnection.java:194) 在 ParseIMDB.parse(ParseIMDB.java:13) 在 ParseIMDB.main(ParseIMDB.java:20)

【问题讨论】:

  • 当我使用 curl 尝试该 URL 时,我实际上得到了 301(永久移动)。添加 www 使 curl 至少返回页面。不确定这是否有帮助。
  • 您是否尝试在代码中添加www?我的浏览器会自动添加www,所以它可能是必需的。
  • 我尝试使用 www,但似乎没有帮助。

标签: java html jsoup


【解决方案1】:

我相信如果您在请求中添加 User-Agent 标头,它会起作用。你可以这样做:

 Document doc = Jsoup.connect("http://imdb.com/search/title?count=100&genres=action&languages=en&release_date=2010,2016&title_type=feature")
                .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")
                .get();

此解决方案已经过测试并且可以正常工作,返回电影列表。

【讨论】:

    【解决方案2】:

    HTTP 403 表示禁止。很可能 imdb 正在阻止编程请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多