2016-06-16 18 views
0

我想制作一个只需将网页作为普通浏览器“访问”的cronjob。在该网页上我有一个脚本:在Crontab中提取包含jQuery帖子的网页

<script> 
var x = 0; 

while(x<10) { 

    setTimeout(
    function() { 

     if ($(".instashow-gallery-media-image img")[0]){ // If image exists 

     var html = $('#instaWrapper').html(); 
     $('#content').val(html); 

     x = 10; // Breaks the loop 

     setTimeout(
     function() { 
      $('#contentSubmit').submit(); 
      }, 2000); // Wait 2 seconds, then submit    

     } else { 
      // Couldn't find the image, trying again 
     } 

     }, 1000); 

     x++; 
    } 
</script> 

如果我在终端卷曲时,JavaScript将无法运行,并张贴永远不会发生。 有没有模拟像Chrome,Safari等实际访问的“卷曲”?或者我可以编写一个可以添加到我的crontab中的Php或Python脚本?

谢谢!

+1

使用像硒这样的东西来模仿人类的行为。 – fedorqui

+0

看起来很酷@fedorqui,但它似乎打开了GUI中的Firefox窗口。我只想要一个基于终端/脚本的解决方案(如果可能的话) – Stichy

+0

您可以使用像幻影一样的无头浏览器 –

回答

0

可以在PHP中使用卷曲,像这样:

<?php 
$ch = curl_init('http://www.google.com/'); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_NOBODY, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Enter your browser agent string here'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); 
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 
if ($status == 200) { 
    // $output contains URLs data, if needed 
} 

如果设置CURLOPT_USERAGENT到您想要的用户代理字符串,那么请求将显示为来自该浏览器。

我想这取决于你想要对数据做什么,无论你只是想ping通网址,还是想实际显示网站,就好像它是浏览器访问它一样。