2016-11-04 7 views
0

いくつかのHTMLを取得して解析するために、urllibを使用して多くの異なるURLにアクセスしようとしています。私はurllibは、約5000回の繰り返しを経る必要がありますループがあります。約100回の反復後npm urllib ResponseTimeoutError - タイムアウトを増やす方法は?

urllib.request('a url here', options=[timeout=50000]).then(function (result) { 
       // data is Buffer instance 
       var $ = cheerio.load(result.data); 
       $('dt').each(function() { 
        var news_html = cheerio.load($(this).html()); 
        if (news_html('span.timestamp').html() != null) { 
         var date = news_html('span.timestamp').html(); 
         var description = news_html('.story_title').html(); 
         var link = news_html('a').attr('href'); 
         var post = {description: description, date: date, link: link}; 
         pool.query('INSERT INTO db SET ?', post, function (err, result) { 
          if (err) { 
           console.log(err); 
          } 
         }); 
        } 
       }); 
      }).catch(function (err) { 
       console.error(err); 
      }); 

を、私はこのエラーを取得する:

{ ResponseTimeoutError: Response timeout for 5000ms, GET http://www.streetinsider.com/stock_lookup_news.php?q=CREG&type=major_news -1 (connected: true, keepalive socket: false) 
headers: {} 
    at Timeout._onTimeout (/Users/max/projects/stock-news-angular/node_modules/urllib/lib/urllib.js:715:15) 
    at tryOnTimeout (timers.js:232:11) 
    at Timer.listOnTimeout (timers.js:202:5) 
    name: 'ResponseTimeoutError', 
    requestId: 595, 
    data: undefined, 
    path: '/stock_lookup_news.php?q=CREG&type=major_news', 
    status: -1, 
    headers: {}, 
    res: 
    { status: -1, 
    statusCode: -1, 
    headers: {}, 
    size: 0, 
    aborted: false, 
    rt: 10030, 
    keepAliveSocket: false, 
    data: undefined, 
    requestUrls: [ 'http://www.streetinsider.com/stock_lookup_news.php?q=CREG&type=major_news' ], 
    timing: null, 
    remoteAddress: '162.242.133.50', 
    remotePort: 80 } } 

は、どのように私は私ができるようにタイムアウトを増やすことについて行くことができますループを終了し、必要なすべてのデータをMySQLデータベースに挿入しますか? npmのurllibには設定するオプションがあるため、タイムアウトを設定する方法を正しく理解していないと思います。

答えて

0

私はオプションを追加すると、request()の機能があなたの問題を解決できると思います。

APIドキュメントで

timeout Number | Array - Request timeout in milliseconds for connecting phase and response receiving phase. Defaults to exports.TIMEOUT, both are 5s. You can use timeout: 5000 to tell urllib use same timeout on two phase or set them seperately such as timeout: [3000, 5000], which will set connecting timeout to 3s and response 5s.

+0

はい、私は、私はそれを設定することになってるか正確にはわかりません。このメソッドは3つの引数をとるようです: 'http.request(url [、options] [、callback])'しかし、私はJS構文に慣れていないので、タイムアウトを追加する方法がわかりません。私は 'urllib.request({url:[url]、options:[timeout = 50000]})'を試しましたが、どちらもうまくいきませんでした。 – needhelpwithR

+0

このよう試みる@needhelpwithR: 'http.request( 'http://example.com'、{ 方法: 'GET'、 データ:{ '' 'ハロー' 'B': 'world' }、 タイムアウト:10000 // 10s }); ' – philipjkim

+0

これを追加すると機能しません。 ECONNRESETエラーが発生しました: 'エラー:TCP.onread(net.js:564:26)で をexports._errnoException(util.js:1026:11) で読んでください。 コード: 'ECONNRESET'、 errno: 'read'、 名前: 'ResponseError'、 ' – needhelpwithR

関連する問題