Results 1 to 3 of 3
Thread: get url returned by search
- 03-12-2011, 08:16 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
get url returned by search
I'm sending a query for Wikipedia site as below:
Java - Wikipedia, the free encyclopedia
The webpage that I'm getting is: Java - Wikipedia, the free encyclopedia
How can I get the URL that the webpage is returning?
Java Code:URL u = new URL("http://en.wikipedia.org/wiki/?search=Java");
- 03-13-2011, 10:45 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 70
- Rep Power
- 0
Just found this: API:Main page - MediaWiki
It is the mediawiki API (Wikipedia is a part of it). Chances are, part of the API is a search function. EDIT: This is what I found. Use this:
This returns possible pages. Pick the correct one, replace spaces with underscores, and go to the wikipedia page.Java Code:http://en.wikipedia.org/w/api.php?action=opensearch&search=Java
EDIT EDIT: Here is a working method and example. It takes the query, and which result number to return (starting from 1, 0 will just give you your query). If it is out of bounds, it return an empty string.
EDITx3: Not sure if u need to replace the spaces. It works w/ spaces in a browser, not sure about Java.Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package urltest; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author phani */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { try { String query = "Java"; System.out.println(searchWikipediaURL(query,1)); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } public static String searchWikipediaURL(String query, int result) throws Exception { try{ URL url = new URL("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + query); InputStream is = url.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String results = br.readLine(); return("http://en.wikipedia.org/wiki/"+results.substring(nthOccurrence(results, '"', result*2)+1, nthOccurrence(results, '"', (result*2)+1))); } catch(Exception e){ return ""; } } public static int nthOccurrence(String str, char c, int n) { int pos = str.indexOf(c, 0); while (n-- > 0 && pos != -1) { pos = str.indexOf(c, pos + 1); } return pos; } }Last edited by rp181; 03-13-2011 at 11:11 PM.
- 03-14-2011, 07:43 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
2 Items returned on itemStateChanged() Test???
By AcousticBruce in forum New To JavaReplies: 1Last Post: 01-08-2011, 04:10 PM -
SplashScreen.getSplashScreen() returned null
By kmm1977 in forum AWT / SwingReplies: 4Last Post: 06-21-2010, 11:04 AM -
SplashScreen.getSplashScreen() returned null
By ibrahimyoussof in forum AWT / SwingReplies: 2Last Post: 04-17-2010, 02:51 PM -
Cant we have two values returned from a method in a class
By jaiminparikh in forum Advanced JavaReplies: 7Last Post: 03-21-2009, 03:20 AM -
Returned type changes if class is extended. How?
By Gelembjuk in forum New To JavaReplies: 10Last Post: 10-31-2008, 11:18 AM


LinkBack URL
About LinkBacks

Bookmarks