【问题标题】:cURL Scrape then Parse/Find Specific ContentcURL 抓取然后解析/查找特定内容
【发布时间】:2015-04-29 02:22:18
【问题描述】:

我正在使用 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

【问题讨论】:

  • 请给我们一些代码,以便我们为您提供帮助。

标签: php parsing curl web-scraping


【解决方案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. 构建和生成输出。 这只是一个关于您想对提取的数据做什么的问题。例如,您可以打印、重新格式化或将其存储到数据库/文件中。

【讨论】:

    【解决方案2】:

    我建议您使用现成的 scaper。我使用 Goutte (https://github.com/FriendsOfPHP/Goutte),它允许我加载网站内容并以与 jQuery 相同的方式遍历它。即如果我想要&lt;div id="content"&gt; 的内容,我使用$client-&gt;filter('#content')-&gt;text()

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

    与使用 cURL 或 file_get_contentsa() 并手动处理 html 相比,它让 mucn 生活变得如此简单

    【讨论】:

    • 感谢您的回复,Horaland,Goutte 可以搜索纯文本吗?因为这个可怕的网站没有 css 类或 id,所以我不得不使用棘手的方法。另外,我研究了 Goutte,但如何设置非常令人困惑。我认为它需要Guzzle,我不熟悉它是如何使用的,请您提供一些启示吗?谢谢!
    • 我使用 Symfony,所以设置 Guzzle 和 Goutte 只是制作“composer require fabpot/goutte”命令的一个例子。它需要某种标签才能工作,但不需要 ID 或类,因此您可以获得 标签或 标签的内容。
    • 所以它不能像preg_match那样进行大小写匹配?我需要做类似preg_match('website.com/page1&amp;id=' . $id); 这样的事情来搜索实际文本,然后找到它包含的标签,然后移动到该标签的下一个孩子。 (因此,如果所需的文本在 &lt;p&gt; 中并找到该标签的下一个出现,并获取其中的内容)
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2017-03-24
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多