Goutte Scrape 登录 https 安全网站

Ken*_*nny 2 ssl curl web-scraping symfony goutte

因此,我尝试使用 Goutte 登录https网站,但出现以下错误:

cURL error 60: SSL certificate problem: unable to get local issuer certificate 500 Internal Server Error - RequestException 1 linked Exception: RingException

这是 Goutte 的创建者说要使用的代码:

use Goutte\Client;

$client = new Client();

$crawler = $client->request('GET', 'http://github.com/');
$crawler = $client->click($crawler->selectLink('Sign in')->link());
$form = $crawler->selectButton('Sign in')->form();
$crawler = $client->submit($form, array('login' => 'fabpot', 'password' =>     'xxxxxx'));
$crawler->filter('.flash-error')->each(function ($node) {
    print $node->text()."\n";
});
Run Code Online (Sandbox Code Playgroud)

或者这里是 Symfony 推荐的代码:

use Goutte\Client;

// make a real request to an external site
$client = new Client();
$crawler = $client->request('GET', 'https://github.com/login');

// select the form and fill in some values
$form = $crawler->selectButton('Log in')->form();
$form['login'] = 'symfonyfan';
$form['password'] = 'anypass';

// submit that form
$crawler = $client->submit($form);
Run Code Online (Sandbox Code Playgroud)

问题是它们都不起作用,我收到了上面发布的错误。我CAN,但在登录使用写在过去的这个问题,我问过的代码:卷曲刮然后解析/查找具体内容

我只想使用 Symfony/Goutte 登录,这样抓取我需要的数据会更容易。请问有什么帮助或建议吗?谢谢!

Ken*_*nny 5

在代码中添加以下内容可修复错误(curl 配置):

    // make a real request to an external site
    $client = new Client();
    $client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYHOST, FALSE);
    $client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYPEER, FALSE);
    $crawler = $client->request('GET', 'https://github.com/login'); 
Run Code Online (Sandbox Code Playgroud)

但随后发生了另一个错误:

The current node list is empty.
500 Internal Server Error - InvalidArgumentException 
Run Code Online (Sandbox Code Playgroud)

再一次,我将 Goutte 与 Symfony 和默认代码一起使用来执行测试任务,例如登录 https github。

上一个错误的修复方法node list empty是 Github 登录页面按钮实际上显示“登录”,而不是按钮上的提交登录。不幸的是,Goutte api 不清楚是$form = $crawler->selectButton('Sign in')->form();指 htmlname属性还是按钮的实际纯文本。显然是纯文本;有点混乱。因此,在对文档记录不佳的 api 进行了更多研究后,我以以下有效的代码结束:

// make a real request to an external site
$client = new Client();
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYHOST, FALSE);
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYPEER, FALSE);
$crawler = $client->request('GET', 'https://github.com/login');

// select the form and fill in some values
$form = $crawler->selectButton('Sign in')->form();
$form['login'] = 'symfonyfan';
$form['password'] = 'anypass';

// submit that form
$crawler = $client->submit($form);
echo $crawler->html();
Run Code Online (Sandbox Code Playgroud)