Jsoup总是返回超时错误

Jsoup总是返回超时错误

问题描述:

我有以下代码从雅虎finance.hk 的滴答价格,但它总是返回超时错误 请帮Jsoup总是返回超时错误

public GetStockPriceFromWebOneByOne(String url){ 
     this.url = url; 
    } 

    private void setDataFromAAStock() throws IOException, InterruptedException{ 
     Document document = Jsoup.connect(url).ignoreHttpErrors(true).timeout(timeOut*1000).get(); // s 
     //TimeUnit.SECONDS.sleep(2); 
     Elements answerers = document.select("div.yfi_rt_quote_summary div.yfi_rt_quote_summary_rt_top.sigfig_promo_0 span.time_rtq_ticker"); 
     // Elements answerers = document.select(".content .inline_block.vat.float_l .boxForex .font26 .neg .arr_ud.arrow_d6"); 
     for (Element answerer : answerers) { 
      //System.out.print(answerer.text()+"\n"); 
      price = answerer.text(); 
      // splitString(answerer.text()); 
     } 
    } 

    public String getDataFromAAStock() throws IOException, InterruptedException{ 
     setDataFromAAStock(); 
     return price; 
    } 

我没有跟雅虎财经香港支票,但你也许应该尝试在连接到它时设置一个合理的浏览器userAgent字符串。见docs

Document document = Jsoup.connect(url) 
         .ignoreHttpErrors(true) 
         .timeout(timeOut*1000) 
         .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6") 

附录: 当然,你也可以通过使用完全关闭超时:

Document document = Jsoup.connect(url) 
         .ignoreHttpErrors(true) 
         .timeout(0) 

难道你看一下网络流量的浏览器,并与该网站之间浏览器开发工具?它可以帮助你分析潜在的问题。

+0

我试图使用userAgent和不使用userAgent方法,都不是wroks –

+0

@WaiHung看到我的修改回答 – luksch

+0

仍然无法正常工作 –