如何从具有特定 class 值的 <a> 标签中提取 href、标题和文本数据?
How to extract href, title, and text data from an <a> tag with a specific class value from scraped html?
我有这个 preg_match_all()
的正则表达式,它在 regex101.com 上正确匹配,但在我的代码上不匹配。
我尝试解析的 html 元素如下所示:
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>
在整个 html curl 结果中找到。每个块都有以下例如:
<li style="opacity: 1;">
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>
<!--<a class="profile-link" href="javascript:void(0)" title="1-844-Iran-Law">Amin Alemohammad</a>-->
<img src="/Images/Uploaded/Photos/4813_1844IranLaw.png" style="max-width:140px; max-height:140px">
<div class="results-profile">
<h2>Amin Alemohammad</h2>
<p><strong>Firm:</strong> 1-844-Iran-Law <br> <strong>Country:</strong> USA</p>
<p class="blue"><strong>Practice Area:</strong> Iranian Desk</p>
<ul>
<li class="tel-icon" style="opacity: 1;">Tel: +1-202-465-8692</li>
<li class="fax-icon" style="opacity: 1;">Fax: +1-202-776-0136</li>
<li class="email-icon">Email: <a style="position:relative; z-index:9999;" href="mailto:amin@1844iranlaw.com">amin@1844iranlaw.com</a></li>
</ul>
</div><!-- results profile -->
<img class="practice-logo" src="/Images/Uploaded/Logos/4813_1844IranLaw.png" style="max-width:185px; max-height:70px;">
<a class="results-btn contact-btn" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="View Full Profile">VIEW FULL PROFILE</a>
<!--<a class="results-btn contact-btn" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">CONTACT</a>-->
<a class="results-btn website-btn" href="http://www.1844iranlaw.com" title="www.1844iranlaw.com">VIEW WEBSITE</a>
</li>
</li>
正则表达式结果
Group 1. 54-58 `4813` // company profile
Group 2. 71-74 `211` // country id
Group 3. 92-93 `0` // practice area
Group 5. 115-129 `1-844-Iran-Law` // company name
Group 6. 131-147 `Amin Alemohammad` // Person's name
我有的是:
preg_match_all('/<a class="profile-link" href="CompanyProfile\.aspx\?PID=(.*?)&country=([0-9]{1,}?)&practicearea=([0-9]{1,10}?)&pagenum=\?" title="(.*?)">(.*?)<\/a>/', $result, $match, PREG_PATTERN_ORDER);
dd($match);
哪个returns
array:6 [▼
0 => []
1 => []
2 => []
3 => []
4 => []
5 => []
]
匹配数正确 -> 字符串模式中有 5 个匹配项,但我不明白为什么它返回空值。
提前感谢您的帮助,因为我尝试了很多方法,但没有找到正确的方法,或者看看我遗漏了什么。
您可以使用 DOMDocument 而不是使用正则表达式。
要从 href
属性中获取值,您可以使用 explode and parse_str.
$html = <<<HTML
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>
HTML;
$doc = new DOMDocument();
$doc->loadHTML($html);
foreach($doc->getElementsByTagName('a') as $a) {
if ($a->getAttribute('class') === 'profile-link') {
$parts = explode('?', $a->getAttribute('href'), 2);
parse_str($parts[1], $output);
echo 'Title: ' . $a->getAttribute('title') . '<br>';
echo 'Text: ' . $a->nodeValue . '<br>';
echo 'PID: ' . $output['PID'];
// etc..
}
}
好吧,在深入研究问题一段时间后,分析了要由 preg_match_all()
解析的整个 html 我只是 git 它通过添加几行来替换来工作html 中的 \t \r \n
因为将其添加到正则表达式中不起作用。
所以解决办法是在preg_match_all()
前加上下面两行:
(...)
$result = curl_exec($curl); // already there
$result = str_replace(["&"], "&", $result); // new
$result = str_replace(["\t", "\r", "\n"], "", $result); // new
$regex = '/<a class="profile-link" href="CompanyProfile\.aspx\?PID=(.*?)&country=([0-9]{1,}?)&practicearea=([0-9]{1,}?)&pagenum=" title="(.*?)">(.*?)<\/a>/s';
preg_match_all($regex, $result, $match, PREG_SET_ORDER);
然后,我没有在 link 中使用 &
,而是在正则表达式中强制使用了 &
字符。效果很好!
感谢所有到过的人伸出援手!
代码:(Demo)
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$output = [];
foreach ($xpath->evaluate("//a[@class='profile-link']") as $node) {
parse_str(parse_url($node->getAttribute('href'), PHP_URL_QUERY), $output);
$output['title'] = $node->getAttribute('title');
$output['text'] = $node->nodeValue;
}
var_export($output);
输出:
array (
'PID' => '4813',
'country' => '211',
'practicearea' => '0',
'pagenum' => '',
'title' => '1-844-Iran-Law',
'text' => 'Amin Alemohammad',
)
我相信这充分利用了 php 语言的全部美感,DomDocument
和 Xpath
到 reliably/directly 目标 tag/node,然后 parse_url()
与 parse_str()
雄辩地将查询字符串数据转换为所需的键值对。
现在您将拥有稳定的东西,没有骇人听闻的 str_replace()
调用或正则表达式模式。
我有这个 preg_match_all()
的正则表达式,它在 regex101.com 上正确匹配,但在我的代码上不匹配。
我尝试解析的 html 元素如下所示:
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>
在整个 html curl 结果中找到。每个块都有以下例如:
<li style="opacity: 1;">
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>
<!--<a class="profile-link" href="javascript:void(0)" title="1-844-Iran-Law">Amin Alemohammad</a>-->
<img src="/Images/Uploaded/Photos/4813_1844IranLaw.png" style="max-width:140px; max-height:140px">
<div class="results-profile">
<h2>Amin Alemohammad</h2>
<p><strong>Firm:</strong> 1-844-Iran-Law <br> <strong>Country:</strong> USA</p>
<p class="blue"><strong>Practice Area:</strong> Iranian Desk</p>
<ul>
<li class="tel-icon" style="opacity: 1;">Tel: +1-202-465-8692</li>
<li class="fax-icon" style="opacity: 1;">Fax: +1-202-776-0136</li>
<li class="email-icon">Email: <a style="position:relative; z-index:9999;" href="mailto:amin@1844iranlaw.com">amin@1844iranlaw.com</a></li>
</ul>
</div><!-- results profile -->
<img class="practice-logo" src="/Images/Uploaded/Logos/4813_1844IranLaw.png" style="max-width:185px; max-height:70px;">
<a class="results-btn contact-btn" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="View Full Profile">VIEW FULL PROFILE</a>
<!--<a class="results-btn contact-btn" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">CONTACT</a>-->
<a class="results-btn website-btn" href="http://www.1844iranlaw.com" title="www.1844iranlaw.com">VIEW WEBSITE</a>
</li>
</li>
正则表达式结果
Group 1. 54-58 `4813` // company profile
Group 2. 71-74 `211` // country id
Group 3. 92-93 `0` // practice area
Group 5. 115-129 `1-844-Iran-Law` // company name
Group 6. 131-147 `Amin Alemohammad` // Person's name
我有的是:
preg_match_all('/<a class="profile-link" href="CompanyProfile\.aspx\?PID=(.*?)&country=([0-9]{1,}?)&practicearea=([0-9]{1,10}?)&pagenum=\?" title="(.*?)">(.*?)<\/a>/', $result, $match, PREG_PATTERN_ORDER);
dd($match);
哪个returns
array:6 [▼
0 => []
1 => []
2 => []
3 => []
4 => []
5 => []
]
匹配数正确 -> 字符串模式中有 5 个匹配项,但我不明白为什么它返回空值。
提前感谢您的帮助,因为我尝试了很多方法,但没有找到正确的方法,或者看看我遗漏了什么。
您可以使用 DOMDocument 而不是使用正则表达式。
要从 href
属性中获取值,您可以使用 explode and parse_str.
$html = <<<HTML
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&country=211&practicearea=0&pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>
HTML;
$doc = new DOMDocument();
$doc->loadHTML($html);
foreach($doc->getElementsByTagName('a') as $a) {
if ($a->getAttribute('class') === 'profile-link') {
$parts = explode('?', $a->getAttribute('href'), 2);
parse_str($parts[1], $output);
echo 'Title: ' . $a->getAttribute('title') . '<br>';
echo 'Text: ' . $a->nodeValue . '<br>';
echo 'PID: ' . $output['PID'];
// etc..
}
}
好吧,在深入研究问题一段时间后,分析了要由 preg_match_all()
解析的整个 html 我只是 git 它通过添加几行来替换来工作html 中的 \t \r \n
因为将其添加到正则表达式中不起作用。
所以解决办法是在preg_match_all()
前加上下面两行:
(...)
$result = curl_exec($curl); // already there
$result = str_replace(["&"], "&", $result); // new
$result = str_replace(["\t", "\r", "\n"], "", $result); // new
$regex = '/<a class="profile-link" href="CompanyProfile\.aspx\?PID=(.*?)&country=([0-9]{1,}?)&practicearea=([0-9]{1,}?)&pagenum=" title="(.*?)">(.*?)<\/a>/s';
preg_match_all($regex, $result, $match, PREG_SET_ORDER);
然后,我没有在 link 中使用 &
,而是在正则表达式中强制使用了 &
字符。效果很好!
感谢所有到过的人伸出援手!
代码:(Demo)
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$output = [];
foreach ($xpath->evaluate("//a[@class='profile-link']") as $node) {
parse_str(parse_url($node->getAttribute('href'), PHP_URL_QUERY), $output);
$output['title'] = $node->getAttribute('title');
$output['text'] = $node->nodeValue;
}
var_export($output);
输出:
array (
'PID' => '4813',
'country' => '211',
'practicearea' => '0',
'pagenum' => '',
'title' => '1-844-Iran-Law',
'text' => 'Amin Alemohammad',
)
我相信这充分利用了 php 语言的全部美感,DomDocument
和 Xpath
到 reliably/directly 目标 tag/node,然后 parse_url()
与 parse_str()
雄辩地将查询字符串数据转换为所需的键值对。
现在您将拥有稳定的东西,没有骇人听闻的 str_replace()
调用或正则表达式模式。