Php 无法使用Symfony Dom爬虫获取元素的值
我正在使用guzzle POST方法获取URL。它正在工作并返回我想要的页面。但问题是,当我想在该页面的表单中获取输入元素的值时,爬虫程序什么也不返回。我不知道为什么 PHP:Php 无法使用Symfony Dom爬虫获取元素的值,php,symfony,dom,guzzle,domcrawler,Php,Symfony,Dom,Guzzle,Domcrawler,我正在使用guzzle POST方法获取URL。它正在工作并返回我想要的页面。但问题是,当我想在该页面的表单中获取输入元素的值时,爬虫程序什么也不返回。我不知道为什么 PHP: <?php use Symfony\Component\DomCrawler\Crawler; use Guzzle\Http\Client; $client = new Client(); $request = $client->get("https://example.com"); $response
<?php
use Symfony\Component\DomCrawler\Crawler;
use Guzzle\Http\Client;
$client = new Client();
$request = $client->get("https://example.com");
$response = $request->send();
$getRequest = $response->getBody();
$cookie = $response->getHeader("Set-Cookie");
$request = $client->post('https://example.com/page_example.php', array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Cookie' => $cookie
), array(
'param1' => 5,
'param2' => 10,
'param3' => 20
));
$response = $request->send();
$pageHTML = $response->getBody();
//fetch orderID
$crawler = new Crawler($pageHTML);
$orderID = $crawler->filter("input[name=orderId]")->attr('value');//there is only one element with this name
echo $orderID; //returns nothing
您不必创建爬虫程序:
$crawler = $client->post('https://example.com/page_example.php', array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Cookie' => $cookie
), array(
'param1' => 5,
'param2' => 10,
'param3' => 20
));
$orderID = $crawler->filter("input[name=orderId]")->attr('value');
这假设您的帖子没有被重定向,如果被重定向,您应该在调用筛选函数之前添加:
$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect();
刚刚测试,它的工作。尝试获取爬虫对象,而不是创建它。(新答案)我使用字符串数组表示法:$string[$I]
和substr()
和substring
函数创建了一个dom爬虫程序来实现这个目标。谢谢你的时间和努力