我正在尝试从一个网站上获取数据。使用以下代码:
@WebServlet(description = "get content from teamforge", urlPatterns = { "/JsoupEx" })
public class JsoupEx extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String URL = "http://www.moving.com/real-estate/city-profile/results.asp?Zip=60505";
public JsoupEx() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Document doc = Jsoup.connect(URL).get();
for (Element table : doc.select("table.DataTbl")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
if (tds.size() > 1) {
System.out.println(tds.get(0).text() + ":"
+ tds.get(2).text());
}
}
}
}
}我正在使用jsoup解析器。当运行时,我没有得到任何错误,只是没有输出。
请在这方面提供帮助。
发布于 2013-02-15 18:51:27
使用以下代码
public class Tester {
private static final String URL = "http://www.moving.com/real-estate/city-profile/results.asp?Zip=60505";
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect(URL).get();
System.out.println(doc);
}
}我得到一个java.net.SocketTimeoutException: Read timed out。我认为你试图抓取的URL对于Jsoup来说太慢了。在欧洲,我的连接可能会比你的慢。但是,您可能希望在AS的日志中检查此异常。
通过将超时设置为10秒,我能够下载并解析文档:
Connection connection = Jsoup.connect(URL);
connection.timeout(10000);
Document doc = connection.get();
System.out.println(doc);使用剩下的代码,我得到了:
人口:78,413
1990年以来的人口变化:53.00%
人口密度:6897
男性:41,137
女性:37278
.
发布于 2013-02-18 13:08:30
谢谢朱利安,我试着用下面的代码获取SocketTimeoutException。代码是
Connection connection=Jsoup.connect("http://www.moving.com/real-estate/city-
profile/results.asp?Zip=60505");
connection.timeout(10000);
Document doc = connection.get();
System.out.println(doc);https://stackoverflow.com/questions/14891259
复制相似问题