本文整理汇总了PHP中Symfony\Component\DomCrawler\Crawler::add方法的典型用法代码示例。如果您正苦于以下问题:PHP Crawler::add方法的具体用法?PHP Crawler::add怎么用?PHP Crawler::add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\DomCrawler\Crawler
的用法示例。
在下文中一共展示了Crawler::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: call
public function call($uri, $method = 'get', $parameters = array(), $changeStack = true)
{
$browser = parent::call($uri, $method, $parameters, $changeStack);
$crawler = new Crawler();
$crawler->add($browser->getResponse()->getContent());
return $crawler;
}
示例2: parse
/**
* @inheritdoc
*/
protected function parse(Requests_Response $requests)
{
$crawler = new Crawler();
$crawler->addContent($requests->body);
$r = $crawler->filterXPath('//*[@id="content"]/div/div[2]/div[1]/div[1]/ul/li');
$results = array();
/** @var DOMElement $el */
foreach ($r as $el) {
$c = new Crawler();
$c->add($el);
$tags = [];
/** @var DOMElement $z */
foreach ($c->filter(".horizontal-separated-list li") as $z) {
$tags[] = $z->textContent;
}
$result = new Result();
$result->setTitle(trim($c->filter(".details a")->text()));
$result->setTags($tags);
$relUrl = $c->filter(".details a")->attr("href");
$id = explode("--", explode("/", parse_url($relUrl)["path"])[2])[1];
$result->setId($this->getName() . "_" . intval($id));
$result->setUrl("http://www.anibis.ch/" . $relUrl);
$result->setPrice($c->filter(".price")->text());
$result->setDescription($c->filter(".details .description")->text());
$results[] = $result;
}
return $results;
}
示例3: testAdd
/**
* @covers Symfony\Component\DomCrawler\Crawler::add
*/
public function testAdd()
{
$crawler = new Crawler();
$crawler->add($this->createDomDocument());
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from a \DOMDocument');
$crawler = new Crawler();
$crawler->add($this->createNodeList());
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from a \DOMNodeList');
foreach ($this->createNodeList() as $node) {
$list[] = $node;
}
$crawler = new Crawler();
$crawler->add($list);
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from an array of nodes');
$crawler = new Crawler();
$crawler->add($this->createNodeList()->item(0));
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from an \DOMNode');
$crawler = new Crawler();
$crawler->add('<html><body>Foo</body></html>');
$this->assertEquals('Foo', $crawler->filter('body')->text(), '->add() adds nodes from a string');
}
示例4: parse
/**
* @inheritdoc
*/
protected function parse(Requests_Response $requests)
{
$crawler = new Crawler();
$crawler->addContent($requests->body);
$r = $crawler->filter("#page > main > section > div > div.result-item-list article a > .box-row");
$results = array();
/** @var DOMElement $el */
foreach ($r as $el) {
$c = new Crawler();
$c->add($el);
$tags = [];
/** @var DOMElement $z */
foreach ($c->filter(".box-row ul.box-row-item-attribute-list li") as $z) {
if ($z->childNodes !== null && $z->childNodes->length >= 4) {
$tags[] = $z->childNodes->item(1)->nodeValue . ": " . $z->childNodes->item(3)->nodeValue;
}
}
$addressB = $c->filter(".item-title--street");
$address = $addressB->text() . " " . $addressB->siblings()->text();
$tags[] = "Adresse: " . $address;
$result = new Result();
$result->setTags($tags);
$result->setTitle(trim($c->filter("h2")->text()));
if ($c->filter("item-description p")->valid()) {
$result->setDescription($c->filter("item-description p")->text());
}
$link = $el->parentNode->attributes->getNamedItem("href")->nodeValue;
$result->setId($this->getName() . "_" . explode("/", $link)[2]);
$result->setUrl("http://m.homegate.ch/" . $link);
$results[] = $result;
}
return $results;
}
示例5: parse
/**
* @param string $path
* @return string
*/
function parse($path)
{
if (!file_exists($this->pathMails = $this->config['cacheDir'] . DIRECTORY_SEPARATOR . $this->siteHash . DIRECTORY_SEPARATOR . $path)) {
mkdir($this->pathMails);
}
foreach ($this->getLinks() as $file => $url) {
$readStream = fopen($url, 'r');
$writeStream = fopen($this->pathSiteHash . DIRECTORY_SEPARATOR . $file, 'w');
stream_set_blocking($readStream, 0);
stream_set_blocking($writeStream, 0);
$read = new \React\Stream\Stream($readStream, $this->loop);
$write = new \React\Stream\Stream($writeStream, $this->loop);
$read->on('end', function () use($file, &$files) {
$path = $this->pathSiteHash . DIRECTORY_SEPARATOR . $file;
$crawler = new Crawler();
$crawler->add(file_get_contents($path));
$arrLinks = $crawler->filter('a')->each(function (Crawler $nodeCrawler) {
return [$nodeCrawler->filter('a')->attr('href')];
});
$validMails = [];
foreach ($arrLinks as $k => $url) {
if (filter_var($url[0], FILTER_VALIDATE_EMAIL)) {
$validMails[] = $url[0];
} else {
if (filter_var($m = str_replace('mailto:', '', $url[0]), FILTER_VALIDATE_EMAIL)) {
$validMails[] = $m;
}
}
}
$mails = [];
foreach ($validMails as $m) {
array_push($mails, str_replace('mailto:', '', $m));
}
file_put_contents($this->pathMails . DIRECTORY_SEPARATOR . $file, implode(PHP_EOL, $mails));
unset($files[$file]);
});
$read->pipe($write);
}
// каждые $this->config['periodTime'] секунд выполнять какое-то действие
$this->loop->addPeriodicTimer($this->config['periodTime'], function ($timer) use(&$files) {
if (0 === count($files)) {
$timer->cancel();
}
echo PHP_EOL . "Passed {$this->config['periodTime']} sec. " . PHP_EOL;
});
echo "This script will show the download status every {$this->config['periodTime']} seconds." . PHP_EOL;
$this->loop->run();
return 'Dir of result in: ' . $this->config['cacheDir'] . DIRECTORY_SEPARATOR . $this->siteHash . DIRECTORY_SEPARATOR . $path;
}
示例6: indexAction
/**
* @Route("/sport/{name}")
* @Template()
*/
public function indexAction($name)
{
$html = "http://www.chemistwarehouse.com.au/search?searchtext=Banana%20Boat%20SPF%2050+%20Everyday%20100g%20Tube&searchmode=allwords";
$ch = curl_init($html);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$crawloer = new Crawler($result);
$products = $crawloer->filter(".Product");
foreach ($products as $product) {
$crawler1 = new Crawler();
$crawler1->add($product);
$productName = $crawler1->filter('a')->attr('title');
$price = $crawler1->filter('.Price')->text();
return ['productName' => $productName, 'price' => $price];
}
}
示例7: getLinks
/**
* @return array
*/
function getLinks()
{
$crawler = new Crawler();
$crawler->add(file_get_contents($this->site));
$arrLinks = $crawler->filter('a')->each(function (Crawler $nodeCrawler) {
return [$nodeCrawler->filter('a')->attr('href')];
});
$validLinks = [];
$i = 0;
foreach ($arrLinks as $k => $url) {
$url[0] = str_replace('/redirect.php?url=', '', $url[0]);
if (!filter_var($url[0], FILTER_VALIDATE_URL)) {
if (@get_headers($url[0])[0] == 'HTTP/1.1 200 OK') {
$validLinks[$i . '.' . $this->config['dataFormat']] = $url[0];
} else {
if (@get_headers($this->site . $url[0])[0] == 'HTTP/1.1 200 OK') {
$validLinks[$i . '.' . $this->config['dataFormat']] = $this->site . $url[0];
}
}
$i++;
}
}
return $validLinks;
}
示例8: testAdd
/**
* @covers Symfony\Component\DomCrawler\Crawler::add
*/
public function testAdd()
{
if (!class_exists('Symfony\\Component\\CssSelector\\CssSelector')) {
$this->markTestSkipped('The "CssSelector" component is not available');
}
$crawler = new Crawler();
$crawler->add($this->createDomDocument());
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from a \\DOMDocument');
$crawler = new Crawler();
$crawler->add($this->createNodeList());
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from a \\DOMNodeList');
foreach ($this->createNodeList() as $node) {
$list[] = $node;
}
$crawler = new Crawler();
$crawler->add($list);
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from an array of nodes');
$crawler = new Crawler();
$crawler->add($this->createNodeList()->item(0));
$this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from an \\DOMNode');
$crawler = new Crawler();
$crawler->add('<html><body>Foo</body></html>');
$this->assertEquals('Foo', $crawler->filter('body')->text(), '->add() adds nodes from a string');
}
示例9: testAddInvalidType
/**
* @expectedException \InvalidArgumentException
*/
public function testAddInvalidType()
{
$crawler = new Crawler();
$crawler->add(1);
}
示例10: removeLastItem
/**
* @param $content string
*
* @return string
*/
private function removeLastItem($content)
{
$document = new \DOMDocument('1.0', \Yii::$app->charset);
$crawler = new Crawler();
$crawler->addHTMLContent($content, \Yii::$app->charset);
$root = $document->appendChild($document->createElement('_root'));
$crawler->rewind();
$root->appendChild($document->importNode($crawler->current(), true));
$domxpath = new \DOMXPath($document);
$crawlerInverse = $domxpath->query(CssSelector::toXPath($this->widgetItem . ':last-child'));
foreach ($crawlerInverse as $key => $elementToRemove) {
$parent = $elementToRemove->parentNode;
$parent->removeChild($elementToRemove);
}
$crawler->clear();
$crawler->add($document);
return $crawler->filter('body')->eq(0)->html();
}
示例11: testAddInvalidNode
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Nodes set in a Crawler must be DOMElement or DOMDocument instances, "DOMNode" given.
*/
public function testAddInvalidNode()
{
$crawler = new Crawler();
$crawler->add(new \DOMNode());
}
示例12: extractAction
public function extractAction($html)
{
$crawler = new Crawler();
$crawler->add($html);
/*$crawler = $crawler->filter('body')->nextAll();
foreach ($crawler as $domElement) {
$nodeValue = $domElement->nodeValue;
}*/
$readData = $crawler->filterXpath('//body/p')->extract(array('_text', 'class'));
/*
* print_r($readData); - Array ( [0] => Array ( [0] => Hello World! [1] => message ) [1] => Array ( [0] => Hello Crawler! [1] => ) [2] => Array
*
* Throw empty data from array readData and create arrays $showDataEven and $showDataOdd
*/
/* Prvi nacin
$i = 0;
$j = 0;
$showData = array();
foreach($readData as $row){
foreach($row as $key=>$value) {
if ($key == 0) {
if(($j % 2) == 0){
$showDataEven[$i] = $value;
$showDataA[$a][$b] = $value;
$b++;
}else{
$showDataOdd[$i] = $value;
$i++;
$showDataA[$a][$b] = $value;
$a++;
$b = 0;
}
}
}
$j++;
}
*/
/*
* $showDataEven: Array ( [0] => Hello World! [1] => Hello World2! [2] => Hello World3! [3] => Hello World4! )
* and $showDataOdd: Array ( [0] => Hello Crawler! [1] => Hello Crawler2! [2] => Hello Crawler3! [3] => Hello Crawler4! )
*/
/*$i = 0;
$j = 0;
foreach($showDataEven as $keyeven=>$valueeven){
$showData[$i][$j] = $valueeven;
$j++;
foreach($showDataOdd as $keyodd=>$valueodd){
if($keyeven == $keyodd){
$showData[$i][$j] = $valueodd;
$j = 0;
}
}
$i++;
}*/
$a = 0;
$b = 0;
/* Drugi nacin */
$showDataA = array();
foreach ($readData as $row) {
foreach ($row as $key => $value) {
if ($key == 0) {
if ($b % 2 == 0) {
$showDataA[$a][$b] = $value;
$b++;
} else {
$showDataA[$a][$b] = $value;
$a++;
$b = 0;
}
}
}
}
//print_r($showDataA);
return $showDataA;
//$crawler = $crawler->filter('body')->children()->text();
//return $crawler;
}
示例13: doRequest
/**
* @param integer $page
*
* @return Crawler
*/
private function doRequest($page)
{
$response = $this->browser->get($this->buildUrl($page));
$crawler = new Crawler();
$crawler->add($response->toDomDocument());
return $crawler;
}
示例14: add
/**
* Adds a node to the current list of nodes.
*
* This method uses the appropriate specialized add*() method based
* on the type of the argument.
*
* Overwritten from parent to allow Crawler to be added
*
* @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node
*
* @api
*/
public function add($node)
{
if ($node instanceof Crawler) {
foreach ($node as $childnode) {
$this->addNode($childnode);
}
} else {
parent::add($node);
}
}
示例15: debugResponse
/**
* Mark the test failed and outputs the HTTP response's body.
*
* @param string $selector
*/
protected function debugResponse($selector = "body")
{
if ($this->getResponseBody()) {
$crawler = new Crawler();
$crawler->add($this->getResponseBody());
$message = "Response debug:\n";
$message .= $crawler->filter($selector)->text();
$this->fail($message);
}
$this->fail("No response to debug");
}