[英]what's wrong in this html dom php code?
I'm trying to do a code that will print the contents of all the elements with itemprop="price"
from some link but it don't work, I can't figure out why, this is the code: 我正在尝试使用
itemprop="price"
从某些链接打印所有元素的内容的代码,但它不起作用,我无法弄清楚原因,这是代码:
<?php
error_reporting(0);
ini_set('display_errors', 0);
$doc = new DOMDocument();
$allscan = array(
'http://www.mobile54.co.il/30786',
'http://www.mobile54.co.il/35873',
'http://www.mobile54.co.il/34722'
);
$alllinks = array();
$html = file_get_contents($allscan[0]);
$doc->loadHTML($html);
$href = $doc->getElementsByTagName('a');
for ($j = 0; $j < count($allscan); $j++) {
$html = file_get_contents($allscan[$j]);
$doc->loadHTML($html);
$href = $doc->getElementsByTagName('a');
for ($i = 0; $i < $href->length; $i++) {
$link = $href->item($i)->getAttribute("href");
$lin = preg_replace('/\s+/', '', 'http://www.mobile54.co.il' . $link . "<br />");
if (strpos($link, 'items/') && !strpos($link, '#techDetailsAName')) {
if (!in_array($lin, $alllinks)) {
$alllinks[] = $lin;
}
}
}
}
for ($i = 0; $i < count($alllinks); $i++) {
echo $alllinks[$i];
}
for ($i = 0; $i < count($alllinks); $i++) {
$lin = "$alllinks[$i]";
$html = file_get_contents($lin);
$doc->loadHTML('<?xml encoding="UTF-8"?>' . $html);
$span = $doc->getElementsByTagName('span');
for ($j = 0; $j < $span->length; $j++) {
$attr = $span->item($j)->getAttribute('itemprop');
if ($attr == "price") {
echo $span->item($j)->textContent . "<br />";
}
}
}
?>
when I paste "someurl" insted of $lin
it work but the other way doesn't. 当我粘贴“someurl” insted的的
$lin
它的工作,但其它方式没有。 I've tried to do $html = file_get_contents($alllinks[$i]);
我试过做
$html = file_get_contents($alllinks[$i]);
but it didn't work, I don't know why. 但它不起作用,我不知道为什么。
I think your problem is probably that you appended a <br />
to the end of your URL for some reason. 我认为您的问题可能是由于某种原因您在网址的末尾添加了
<br />
。 But, there are a lot of opportunities to improve your code with use of XPath . 但是,使用XPath有很多改进代码的机会。 (Note also that you can just pass a URL directly to the DomDocument object.)
(另请注意,您只需将URL直接传递给DomDocument对象。)
First we pull all the <a>
elements with matching attribute values . 首先,我们使用匹配的属性值拉出所有
<a>
元素。 We get the URLs and then search them for elements with the exactly matching itemprop
attribute, and get the text content of them. 我们获取URL,然后搜索具有完全匹配的
itemprop
属性的元素,并获取它们的文本内容 。
<?php
$url = "http://www.mobile54.co.il/30786";
$prices = [];
$hrefs = [];
$combined = [];
$dom = new DomDocument;
libxml_use_internal_errors(true);
$dom->loadHtmlFile($url);
$xpath = new DomXPath($dom);
// get <a> elements with href containing items/ but not #techDetailsAName
$nodes = $xpath->query("//a[contains(@href, 'items/') and not(contains(@href, '#techDetailsAName'))]/@href");
foreach ($nodes as $node) {
$hrefs[] = trim($node->value);
}
// now you have a list of URLs
foreach ($hrefs as $k=>&$href) {
$href = "http://www.mobile54.co.il$href";
$dom->loadHtmlFile($href);
$xpath = new DomXPath($dom);
// get any element with itemprop of price
$nodes = $xpath->query("//*[@itemprop='price']");
$prices[$k] = $nodes->item(0)->textContent;
}
// now you have $urls and $prices, combine them:
foreach ($hrefs as $k=>$v) {
$combined[$k] = [$hrefs[$k], $prices[$k]];
}
print_r($combined);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.