我试着在每个美国城市建立维基百科页面。因为我不知道实际的URL,所以我进行搜索并加载第一个结果。这样做的URL签名是:
http://en.wikipedia.org/wiki/Special:Search?go=Go&search=New+York%2C+NY
但是,它没有得到任何回报,下面是我的代码:
String curWikiURL = "http://en.wikipedia.org/wiki/Special:Search?go=Go&search="+URLEncoder.encode("New York, NY", "UTF-8");;
Scanner scanner = null;
URLConnection connection = null;
connection = new URL(curWikiURL).openConnection();
scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
content = scanner.next();
Document doc = Jsoup.parse(content);发布于 2015-06-23 14:08:37
您不必做所有的连接,JSoup库可以在下面处理所有的these.Check
String url = "https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=jsonfm ";
org.jsoup.nodes.Document document = (org.jsoup.nodes.Document) Jsoup
.connect(url).followRedirects(false).timeout(60000).get();
org.jsoup.select.Elements elements = ((org.jsoup.nodes.Document) document)
.body().children();
for (Element element : elements) {
System.out.println(element);
}发布于 2015-06-23 13:57:38
如下所示使用它:
https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=jsonfm 这就是使用MediaWiki API的方式。
查看这里获取更多详细信息- page
https://stackoverflow.com/questions/31004617
复制相似问题