如何从<a> tag with a specific class value from scraped html?

时间:2018-04-26 13:56:42

标签: php regex dom preg-match-all domparser

I've this regex expression for the preg_match_all() that matches correctly on regex101.com but not on my code.

The html element I'm trying to parse looks like this:

<a class="profile-link" href="CompanyProfile.aspx?PID=4813&amp;country=211&amp;practicearea=0&amp;pagenum=" title="1-844-Iran-Law">Amin Alemohammad</a>

Which is found int the whole html curl result. Each block has the following eg.:

<li style="opacity: 1;">
   <a class="profile-link" href="CompanyProfile.aspx?PID=4813&amp;country=211&amp;practicearea=0&amp;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&amp;country=211&amp;practicearea=0&amp;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>

The regex result

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

What I have is:

preg_match_all('/<a class="profile-link" href="CompanyProfile\.aspx\?PID=(.*?)&amp;country=([0-9]{1,}?)&amp;practicearea=([0-9]{1,10}?)&amp;pagenum=\?" title="(.*?)">(.*?)<\/a>/', $result, $match, PREG_PATTERN_ORDER);
dd($match);

which returns

array:6 [▼
   0 => []
   1 => []
   2 => []
   3 => []
   4 => []
   5 => []
]

The number of matches are correct -> 5 matches in the string pattern but what I can't figure out is why it's returning empty values.

Thanks for any help in advance as I've tried so many approaches but for not the correct one or seeing what am I missing.

3 个答案:

答案 0 :(得分:1)

您可以使用DOMDocument而不是使用正则表达式。

要获取href属性中的值,您可以使用explodeparse_str

$html = <<<HTML
<a class="profile-link" href="CompanyProfile.aspx?PID=4813&amp;country=211&amp;practicearea=0&amp;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..
    }
}

Demo

答案 1 :(得分:1)

代码:(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 DomDocumentXpath的完美结合,可靠/直接定位符合条件的标记/节点,然后parse_url()parse_str()一起使用雄辩地将查询字符串数据转换为所需的键值对。

现在,您将拥有一些稳定的东西,没有hacky str_replace()调用或正则表达式模式。

答案 2 :(得分:0)

好吧,经过一段时间深入研究问题,分析整个html要由preg_match_all()解析我只是通过添加几行代替html中的\t \r \n来实现它的工作将它添加到正则表达式不起作用。

所以解决方案是在preg_match_all()之前添加以下两行:

(...)
$result = curl_exec($curl); // already there

$result = str_replace(["&amp;"], "&", $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);

然后,我没有在链接中&amp;强制使用正则表达式中的&字符。它像魅力一样工作!

比你们所有在场的所有人一样!