【问题标题】:PHP - Web scraping - How to cache using cURL?PHP - Web 抓取 - 如何使用 cURL 进行缓存?
【发布时间】:2014-01-31 12:30:35
【问题描述】:

我有一个包含 url 列表的脚本,我正在从这些 url 中获取信息,例如姓名、城市、部门等

这些是我的一些功能:

function getCity($url)
    {
    $url = curl_get_contents($url);
    $html_object = str_get_html($url);
    return $ret = $html_object->find('td', 86)->plaintext;
    }

function getDepartment($url)
    {
    $url = curl_get_contents($url);
    $html_object = str_get_html($url);
    return $ret = $html_object->find('td', 90)->plaintext;
    }

function getSalary($url)
    {
    $url = curl_get_contents($url);
    $html_object = str_get_html($url);
    $ret = $html_object->find('td', 94)->plaintext;
    return trim($ret);
    }

这是我的 cURL 代码:

function curl_get_contents($url)
{
  $curl_moteur = curl_init();
  curl_setopt($curl_moteur, CURLOPT_URL, $url);
  curl_setopt($curl_moteur, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($curl_moteur,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

  curl_setopt($curl_moteur, CURLOPT_FOLLOWLOCATION, 1);
  $web = curl_exec($curl_moteur);
  curl_close($curl_moteur);
  return $web;
}

如您所见,我正在为每个字段发出请求,这是非常低效的。 我想实现一个缓存,以便一次提取请求每个 url 的所有信息字段。

提前致谢。

【问题讨论】:

  • 只需将 $web return 保存在一个文件中,并在需要的时间打开它。

标签: php curl web-scraping


【解决方案1】:

你可以像这样从你的函数创建一个类:

  class Scrapper
{
    public $page_content;

    public $html_object;

    public function __construct($url)
    {
        $this->page_content = $this->curl_get_contents($url); //in case you want to keep for something scrapped url content
        $this->html_object  = $this->str_get_html($this->page_content); //create object from html, probably simpleXML
    }

    public function getCity()
    {
        return $this->html_object->find('td', 86)->plaintext;
    }

    public function getDepartment()
    {
        return $this->html_object->find('td', 90)->plaintext;
    }

    public function getSalary()
    {

        $ret = $this->html_object->find('td', 94)->plaintext;
        return trim($ret);
    }

    public function curl_get_contents($url)
    {
        $curl_moteur = curl_init();
        curl_setopt($curl_moteur, CURLOPT_URL, $url);
        curl_setopt($curl_moteur, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl_moteur,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        curl_setopt($curl_moteur, CURLOPT_FOLLOWLOCATION, 1);
        $web = curl_exec($curl_moteur);
        curl_close($curl_moteur);
        return $web;
    }

    public function str_get_html()
    {
        //unkown function content
        $this->html_object = $some_object; // $some_object = str_get_html($url) from your code;
    }
}

$scrapper = new Scrapper($your_url);

echo $scrapper->getCity();
echo $scrapper->getDepartment();

请注意,代码未经测试。

这样你在实例化类时请求一次 url。

或者如果您不想使用对象,一个简单的解决方法是使用static 变量:

function curl_get_contents($url)
{
  static $web = null;
  if (!is_null($web)) {
     return $web;
  }

  $curl_moteur = curl_init();
  curl_setopt($curl_moteur, CURLOPT_URL, $url);
  curl_setopt($curl_moteur, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($curl_moteur,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

  curl_setopt($curl_moteur, CURLOPT_FOLLOWLOCATION, 1);
  $web = curl_exec($curl_moteur);
  curl_close($curl_moteur);
  return $web;
}

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 2014-11-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多