【问题标题】:PHP Screen Scraping and SessionsPHP 屏幕抓取和会话
【发布时间】:2010-12-06 01:29:02
【问题描述】:

好吧,对屏幕抓取来说还是个新手。

我已经成功登录到我需要的网站,但现在如何重定向到另一个页面? 登录后,我试图在我需要的页面上执行另一个 GET 请求,但它有一个重定向,将我带回登录页面。

所以我认为 SESSION 变量没有被传递,我该如何解决这个问题?

问题:

即使我发布了第二页 URL,它仍然会将我重定向回登录页面,除非我已经登录,但屏幕抓取代码不允许传递 SESSION 数据?

我从another screen scraper question here @stack找到了这段代码

class Curl {

    public $cookieJar = "";

    public function __construct($cookieJarFile = 'cookies.txt') {
        $this->cookieJar = $cookieJarFile;
    }

    function setup() {
        $header = array();
        $header[0]  = "Accept: text/xml,application/xml,application/xhtml+xml,";
        $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[]   = "Cache-Control: max-age=0";
        $header[]   = "Connection: keep-alive";
        $header[]   = "Keep-Alive: 300";
        $header[]   = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[]   = "Accept-Language: en-us,en;q=0.5";
        $header[]   = "Pragma: "; // browsers keep this blank.

        curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieJar);
        curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieJar);
        curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    }

    function get($url) {
        $this->curl = curl_init($url);
        $this->setup();

        return $this->request();
    }

    function getAll($reg, $str) {
        preg_match_all($reg, $str, $matches);
        return $matches[1];
    }

    function postForm($url, $fields, $referer = '') {
        $this->curl = curl_init($url);
        $this->setup();
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_REFERER, $referer);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
        return $this->request();
    }

    function getInfo($info) {
        $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
        return $info;
    }

    function request() {
        return curl_exec($this->curl);
    }
}

调用类

include('/var/www/html/curl.php');
$curl = new Curl();

$url = "here.com";
$newURL = "here.com/newpage.php";

$fields = "usr=user1&pass=PassWord";

// Calling URL
$referer = "http://here.com/index.php";

$html = $curl->postForm($url, $fields, $referer);
$html = $curl->get($newURL);

echo $html; // takes me back to $url instead of $newURL

【问题讨论】:

    标签: php session curl screen-scraping


    【解决方案1】:

    也许这个例子不正确..但从它的外观来看,域正在改变..所以 here.com 会话不会在 there.com 上存在

    【讨论】:

    • SESSION 应该从 here.com 传递到 there.com,它是同一个域,只是一个不同的页面,但只是我使用的示例。也许我应该改变它
    【解决方案2】:

    该站点可能正在尝试将会话 ID 存储在 cookie 中。不过,您已通过“cookies.txt”文件设置 curl 以使用 cookie。所以,我的第一个想法是——cookies.txt 文件中有什么?该脚本是否有权实际创建该文件?

    【讨论】:

      【解决方案3】:

      以下行不使用 "$this" 并且 $cookieJar 不在本地范围内:

      curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieJar);
      curl_setopt($this->curl, CURLOPT_COOKIEFILE, $cookieJar);
      

      所以它应该是这样的:

          curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar);
          curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar);
      

      如果这不能解决问题,请尝试并仅发布帖子:

      $curl->postForm($url, $fields, $referer);

      而不是

      $curl->get($newURL)

      然后检查 cookie.txt 文件是否包含任何内容?它会被创建吗?让我们知道结果,因为在没有实际 URL 的情况下很难快速测试您的代码。

      如果它没有创建 cookie.txt 文件,那么您几乎可以保证会话不会在请求之间保持。

      【讨论】:

      • 谢谢这是问题以及 cookies.txt 文件没有正确的权限。这样的新手错误。再次感谢
      【解决方案4】:

      使用 $curl->get($newURL) 而不是 $curl->postForm($url, $fields, $referer); 可以正常工作;

      【讨论】:

        猜你喜欢
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 2010-09-16
        相关资源
        最近更新 更多