用于PHP的报废库-phpQuery? [英] Scraping Library for PHP - phpQuery?

查看:120
本文介绍了用于PHP的报废库-phpQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个PHP库,该库允许我剪贴网页并处理所有cookie,并使用默认值预填充表单,这是让我最烦的事情.

我已经厌倦了必须将每个输入元素与xpath匹配的情况,如果有更好的东西,我将很高兴.我遇到过 phpQuery ,但手册内容不太清楚,我无法找到发出POST请求的方法.

有人可以帮助我吗?谢谢.

@Jonathan Fingland:

在手册中为browserGet()提供的示例中,我们有:

require_once('phpQuery/phpQuery.php');

phpQuery::browserGet('http://google.com/', 'success1');

function success1($browser)
{
    $browser->WebBrowser('success2')
    ->find('input[name=q]')->val('search phrase')
    ->parents('form')
    ->submit();
}

function success2($browser)
{
    echo $browser;
}

我想所有其他字段都被废弃,并在GET请求中发送回去,我想对phpQuery :: browserPost()方法执行相同的操作,但我不知道该怎么做.我尝试抓取的表单具有输入令牌,如果phpQuery能足够聪明地抓取令牌并让我更改其他字段(在本例中为用户名和密码),并通过POST提交所有内容,我将很高兴.

PS :请放心,这不会将用于垃圾邮件.

解决方案

请参见 http://code.google.com/p/phpquery/wiki/Ajax ,尤其是:

phpQuery::post($url, $data, $callback, $type)

# data Object, String将数据参数定义为对象或字符串. POST请求应该使用查询字符串格式,例如:

$data = "username=Jon&password=123456";
$url = "http://www.mysite.com/login.php";
phpQuery::post($url, $data, $callback, $type)

由于phpQuery是jQuery端口,因此方法签名是相同的(文档直接链接到jquery站点- phpQuery::browserPost 函数可能会更好地满足您的需求.

但是,还请注意,仅在 解决方案

See http://code.google.com/p/phpquery/wiki/Ajax and in particular:

phpQuery::post($url, $data, $callback, $type)

and

# data Object, String which defines the data parameter as being either an Object or a String. POST requests should be possible using query string format, e.g.:

$data = "username=Jon&password=123456";
$url = "http://www.mysite.com/login.php";
phpQuery::post($url, $data, $callback, $type)

as phpQuery is a jQuery port the method signature is the same (the docs link directly to the jquery site -- http://docs.jquery.com/Ajax/jQuery.post)

Edit

Two things:

There is also a phpQuery::browserPost function which might meet your needs better.

However, also note that the success2 callback is only called on the submit() or click() methods so you can fill in all of the form fields prior to that.

e.g.

require_once('phpQuery/phpQuery.php');
phpQuery::browserGet('http://www.mysite.com/login.php', 'success1');
function success1($browser) {
  $handle = $browser
    ->WebBrowser('success2');
  $handle 
    ->find('input[name=username]')
      ->val('Jon');
  $handle 
    ->find('input[name=password]')
      ->val('123456');
      ->parents('form')
        ->submit();
}
function success2($browser) {
  print $browser;
}

(Note that this has not been tested, but should work)

这篇关于用于PHP的报废库-phpQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆