2016-11-23 74 views
-1

所以我有一个程序从Yelp中提取商业信息并输出。一切都编译完成,并运行一段时间,直到它最终遇到java.net.SocketTimeoutException。我对这个问题做了一些研究,显然这是网络的一个问题,解决办法是增加一个运行时间超时。这是事情,我不知道如何完成,也没有如何将其实现到我的代码。这是我得到的:如何添加运行时超时以防止出现java.net.SocketTimeoutException?

import java.util.ArrayList; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 
import java.io.IOException; 
import java.util.Scanner; 

public class YelpScraper 
{ 
    public static void main(String[] args) throws IOException, Exception, RuntimeException 
    {   
     //Variables 
     String description; 
     String location; 
     int pages; 
     int parseCount = 0; 
     Document document; 

     Scanner keyboard = new Scanner(System.in); 

     //Perform a Search 
     System.out.print("Enter a description: "); 
     description = keyboard.nextLine(); 

     System.out.print("Enter a state: "); 
     location = keyboard.nextLine(); 

     System.out.print("How many pages should we scan? "); 
     pages = keyboard.nextInt(); 

     String descString = "find_desc=" + description.replace(' ', '+') + "&"; 
     String locString = "find_loc=" + location.replace(' ', '+') + "&"; 
     int number = 0; 

     String url = "https://www.yelp.com/search?" + descString + locString + "start=" + number; 
     ArrayList<String> names = new ArrayList<String>(); 
     ArrayList<String> address = new ArrayList<String>(); 
     ArrayList<String> phone = new ArrayList<String>(); 

     //Fetch Data From Yelp 
     for (int i = 0 ; i <= pages ; i++) 
     { 

      document = Jsoup.connect(url).get(); 

      Elements nameElements = document.select(".indexed-biz-name span"); 
      Elements addressElements = document.select(".secondary-attributes address"); 
      Elements phoneElements = document.select(".biz-phone"); 

      for (Element element : nameElements) 
      { 
       names.add(element.text()); 
      } 

      for (Element element : addressElements) 
      { 
       address.add(element.text()); 
      } 

      for (Element element : phoneElements) 
      { 
       phone.add(element.text()); 
      } 

      for (int index = 0 ; index < 10 ; index++) 
      { 
       System.out.println("\nLead " + parseCount); 
       System.out.println("Company Name: " + names.get(parseCount)); 
       System.out.println("Address: " + address.get(parseCount)); 
       System.out.println("Phone Number: " + phone.get(parseCount)); 

       parseCount = parseCount + 1; 
      } 

      number = number + 10; 

     } 
    } 
} 

我需要做什么来增加运行时间超时?

+0

什么导致了你,你需要一个运行超时的结论呢? – ControlAltDel

回答

相关问题