0

I am working on a project. I need to figure out the state and operator of the phone number entered by user. I tried making the url connection using jsoup and tried to retrieve the location and operator. But it is not working.

Code is:

 public class WebScraper { public static void main(String args[]) throws IOException { //String url; String mobileNumber = "9566171277"; String url ="http://trace.bharatiyamobile.com/?numb="+mobileNumber; Document doc = Jsoup.connect(url).get(); Elements links = doc.select("span.bluetext"); // Elements media = doc.select("[src]"); // Element imports = doc.select("div.eachStory").first(); //Elements resultLinks = doc.select("table>tbody>tr"); // PrintWriter out = new PrintWriter(new BufferedWriter(new // FileWriter("Data.txt", true))); for (int i = 0; i < links.size(); i++) { System.out.println(links.get(i).text()); // out.println(resultLinks.get(i)); } } } 

Edit: I changed the title, since the solution of the problem has in fact nothing to do with the specifics of the old tilte. It was

How to get location and operator of mobile number using java program?

1
  • 1
    What do you expect? What is the error you get? Any StackTrace that you can share here? Commented Sep 24, 2015 at 7:35

1 Answer 1

3

I looked at the network traffic in the browser and compared it to the document that you get with your code. It turns out that the webserver uses the userAgent string for something and it must be set to a common webbrowser. Fortunately you can do this in Jsoup. If you try this it works:

String ua = "Mozilla/5.0 ;Windows NT 6.1; " + "WOW64; AppleWebKit/537.36 ;KHTML, like Gecko; " + "Chrome/39.0.2171.95 Safari/537.36"; Document doc = Jsoup.connect(url) .userAgent(ua) .get(); Elements links = doc.select("span.bluetext"); 

Common userAgent strings can be found on many places in the web, e.g. http://whatsmyuseragent.com/CommonUserAgents or http://www.useragentstring.com/pages/Firefox/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.