使用 if 语句通过 Xpath 抓取数据 [英] Using if statement to grab a data with Xpath
问题描述
我有三个 URL,这些 URL 包含我想要的数据.但是每个数据在不同的html标签中.所以这就是为什么我不能为所有人提供相同的 Xpath.我需要尝试如果没有找到这个 Xpath,那么试试这个."就像一种方式.但我对如何做到这一点感到有些困惑?
I have three URL and those URLs have the data that I want. But each data in the different html tag. So that's why I can't give the same Xpath for all. I need to try "if not find it this Xpath then try this." like a way. But i am bit confused to how to do that?
例如,这些是链接$linkBox
:
array(3) {
[0]=>
string(34) "https://lions-mansion.jp/MF161026/"
[1]=>
string(34) "https://lions-mansion.jp/MF171045/"
[2]=>
string(34) "https://lions-mansion.jp/MF171010/"
}
我将一一访问这些链接.对于第一个.我给 Xpath:
And I am going in those links one by one. And for the first one. I am giving Xpath:
$get = [];
foreach ($linkBox as $box){
$content = pageContent($box);
$Pars = new \DOMXPath($content);
$Route = $Pars->query("//ul[@id='snav']/li/a");
foreach ($Route as $Rot){
$get = $Rot->getAttribute('href');
}
}
但是那个 Xpath 不适合第二个或第三个.所以用 if 语句如果它是空的我怎么写试试这个?像代码?我能做到吗?或者我需要使用其他方式吗?
But that Xpath doesn't correct for second or third one. So with if statement how can I write if it's null try this? like a code? Can I make it? or Do I need to use another way?
第二个 Box 的 Xpath 是:
The second Box's Xpath is:
$Route = $Pars->query("//nav[@id='siteActionNav']ul/li/a");
第二个 Box 的 Xpath 是:
The second Box's Xpath is:
$Route = $Pars->query("//ul[@id='subNavi']/li[2]/a");
推荐答案
您可以做的是尝试每个 XPath 表达式并查看它是否返回任何元素.
What you can do is try out each XPath expression and see if it returns any elements.
例如,这里有一个函数依次测试每个表达式,如果找到任何匹配项,则返回 DOMNodeList
,否则抛出异常...
For example, here's a function that tests each expression in turn, returning a DOMNodeList
if it finds any matches, throwing an exception otherwise...
function findLinks(\DOMXPath $xp) {
$queries = [
'//ul[@id="snav"]/li/a',
'//nav[@id="siteActionNav"]ul/li/a',
'//ul[@id="subNavi"]/li[2]/a'
];
foreach ($queries as $query) {
$links = $xp->query($query);
if ($links->length > 0) {
return $links; // exits the function and returns the list
}
}
throw new \RuntimeException('No links found');
}
然后你可以像这样使用
foreach ($linkBox as $box){
$content = pageContent($box);
try {
$links = findLinks(new \DOMXPath($content));
foreach ($links as $link){
$get[] = $link->getAttribute('href'); // note: changed to a push
}
} catch (\Exception $e) {
echo "Problem with $box: " . $e->getMessage();
}
}
这篇关于使用 if 语句通过 Xpath 抓取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!