php - cURL 抓取然后解析/查找特定内容

标签 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)

最佳答案

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

  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. 构建并生成输出。 这只是一个您想如何处理提取的数据的问题。例如,您可以打印它、重新格式化它或将其存储到数据库/文件中。

关于php - cURL 抓取然后解析/查找特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28747006/

相关文章:

php - 使用PHP从网页获取json数据

java - 使用 SAXParser 检查多个标签

file - Yii2 - 日志文件 (app.log) 以及如何访问它们?

Python - curl 请求 - 'data-urlencode'

java - 尝试上传图像时,base64_decode 返回 null

php - mysql 不断返回空结果(错误)

php - 尝试在 kitematic 上为 docker 设置 lamp 和 wordpress

php - 根据日期对 MySQL 查询结果进行排序

java - 如何解析像 Ant 一样的路径

C++ 和 cUrl : how to get SSL error codes