Jsoup 在指定网站上无法正常工作
Jsoup not working properly on specified website
这是一个中文网站。(Link)
我想用Jsoup来解析这个网站,但是Jsoup好像不能用。
一个非常简单的代码:
Document doc = Jsoup.connect("http://pchome.megatime.com.tw/stock/sid1101.html")
.timeout(0).get();
Elements links = doc.select("a");
for(Element e : links) {
System.out.println(e.text());
}
什么也没有出来。
我的Jsoup 可以解析除了这个以外的所有网站。谁能帮我解决这个问题?
网站正在做一件有趣的事情,首先它 returns 重定向(http 代码 302),然后 returns 一个迷你页面,该页面使用 is_check=1
参数提交表单。我们必须遵循所有这些步骤。
此外,您需要指定一个用户代理。
总结一下,就是:
Response res = Jsoup.connect("http://pchome.megatime.com.tw/stock/sid1101.html")
.followRedirects(false)
.timeout(0)
.method(Method.GET)
.header("User-Agent", "Mozilla/5.0")
.execute();
String location = res.header("Location");
res = Jsoup.connect("http://pchome.megatime.com.tw/stock/sid1101.html")
.timeout(0)
.data("is_check", "1")
.method(Method.POST)
.header("User-Agent", "Mozilla/5.0")
.header("Referer", location)
.execute();
Document doc = res.parse();
Elements links = doc.select("a");
for(Element e : links) {
System.out.println(e.text());
}
你会得到很多链接。
这是一个中文网站。(Link)
我想用Jsoup来解析这个网站,但是Jsoup好像不能用。
一个非常简单的代码:
Document doc = Jsoup.connect("http://pchome.megatime.com.tw/stock/sid1101.html")
.timeout(0).get();
Elements links = doc.select("a");
for(Element e : links) {
System.out.println(e.text());
}
什么也没有出来。
我的Jsoup 可以解析除了这个以外的所有网站。谁能帮我解决这个问题?
网站正在做一件有趣的事情,首先它 returns 重定向(http 代码 302),然后 returns 一个迷你页面,该页面使用 is_check=1
参数提交表单。我们必须遵循所有这些步骤。
此外,您需要指定一个用户代理。
总结一下,就是:
Response res = Jsoup.connect("http://pchome.megatime.com.tw/stock/sid1101.html")
.followRedirects(false)
.timeout(0)
.method(Method.GET)
.header("User-Agent", "Mozilla/5.0")
.execute();
String location = res.header("Location");
res = Jsoup.connect("http://pchome.megatime.com.tw/stock/sid1101.html")
.timeout(0)
.data("is_check", "1")
.method(Method.POST)
.header("User-Agent", "Mozilla/5.0")
.header("Referer", location)
.execute();
Document doc = res.parse();
Elements links = doc.select("a");
for(Element e : links) {
System.out.println(e.text());
}
你会得到很多链接。