cURL Scrape然后解析/查找特定内容

时间:2015-02-26 15:57:52

标签: php parsing curl web-scraping

我使用php和cURL来抓取一个网页,但是这个网页的设计很糟糕(因为没有类或标签上的ID),所以我需要搜索特定的文本,然后转到标签持有它(即<p>)然后移动到下一个孩子(或下一个<p>)并获取文本。

我需要从页面中获取各种内容,有些内容也是<a onclick="get this stuff here">中的文本。所以基本上我觉得我需要使用cURL将源代码刮到php变量,然后我可以使用php来解析并找到我需要的东西。

这听起来像是最好的方法吗?有没有人有任何指针或可以演示如何将源代码从cURL放入变量?

谢谢!

编辑(工作/当前代码)-----------

<?php

class Scrape
{
public $cookies = 'cookies.txt';
private $user = null;
private $pass = null;

/*Data generated from cURL*/
public $content = null;
public $response = null;

/* Links */
private $url = array(
                    'login'      => 'https://website.com/login.jsp',
                    'submit'     => 'https://website.com/LoginServlet',
                    'page1'      => 'https://website.com/page1',
                    'page2'      => 'https://website.com/page2', 
                    'page3'      => 'https://website.com/page3'
                    );

/* Fields */
public $data = array();

public function __construct ($user, $pass)
{

    $this->user = $user;
    $this->pass = $pass;

}

public function login()
{

            $this->cURL($this->url['login']);

            if($form = $this->getFormFields($this->content, 'login'))
            {
                $form['login'] = $this->user;
                $form['password'] =$this->pass;
                // echo "<pre>".print_r($form,true);exit;
                $this->cURL($this->url['submit'], $form);
                //echo $this->content;//exit;
            }
           //echo $this->content;//exit;
}

// NEW TESTING
public function loadPage($page)
{
            $this->cURL($this->url[$page]);
            echo $this->content;//exit;
}

/* Scan for form */
private function getFormFields($data, $id)
{
        if (preg_match('/(<form.*?name=.?'.$id.'.*?<\/form>)/is', $data, $matches)) {
            $inputs = $this->getInputs($matches[1]);

            return $inputs;
        } else {
            return false;
        }

}

/* Get Inputs in form */
private function getInputs($form)
{
    $inputs = array();

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

    if ($elements > 0) {
        for($i = 0; $i < $elements; $i++) {
            $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

            if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                $name  = $name[1];
                $value = '';

                if (preg_match('/value=(?:["\'])?([^"\']*)/i', $el, $value)) {
                    $value = $value[1];
                }

                $inputs[$name] = $value;
            }
        }
    }

    return $inputs;
}

/* Perform curl function to specific URL provided */
public function cURL($url, $post = false)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, 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");
        // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookies);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);

    if($post)   //if post is needed
    {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    }

    curl_setopt($ch, CURLOPT_URL, $url);
    $this->content = curl_exec($ch);
    $this->response = curl_getinfo( $ch );
    $this->url['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
}
}


$sc = new Scrape('user','pass');
$sc->login();

$sc->loadPage('page1');
echo "<h1>TESTTESTEST</h1>";

$sc->loadPage('page2');
echo "<h1>TESTTESTEST</h1>";

$sc->loadPage('page3');
echo "<h1>TESTTESTEST</h1>";

(注意:归功于@Ramz scrape a website with secured login

2 个答案:

答案 0 :(得分:1)

您可以将问题分成几个部分。

  1. 从数据源中检索数据。 为此,您可以根据需要使用CURL或file_get_contents()。代码示例无处不在。 http://php.net/manual/en/function.file-get-contents.phphttp://php.net/manual/en/curl.examples-basic.php

  2. 解析检索到的数据。 为此,我将首先研究“PHP Simple HTML DOM Parser”您可以使用它从HTML字符串中提取数据。 http://simplehtmldom.sourceforge.net/

  3. 构建并生成输出。 这只是您要对提取的数据执行的操作的问题。例如,您可以打印,重新格式化或将其存储到数据库/文件中。

答案 1 :(得分:1)

我建议你使用已经制造的scaper。我使用Goutte(https://github.com/FriendsOfPHP/Goutte),它允许我加载网站内容并以与jQuery相同的方式遍历它。即如果我想要<div id="content">我使用$client->filter('#content')->text()

的内容

它甚至允许我查找并“点击”链接并提交表单以检索和处理内容。

使用生活方式比使用cURL或file_get_contentsa()更容易,并且手动完成html

相关问题