【发布时间】: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,但似乎没有帮助。