如何从html页面获取div类?

时间:2017-10-24 12:41:13

标签: php html

我有这部分HTML代码:

<h1 class="float_left margin_right margin_bottom">Niccolò Campriani</h1>
    <div class="clear_left"></div>
    <p><span class="bold_text">Full name:</span> Niccolò Campriani
    <br><span class="bold_text">Nickname(s):</span> Nicco
    <br><span class="bold_text">Gender:</span> Male
    <br><span class="bold_text">Height:</span> 5-9.5 (177 cm)
    <br><span class="bold_text">Weight:</span> 176 lbs (80 kg)
    <br><span class="bold_text">Born:</span> <span id="necro-birth" data-birth="1987-11-06"><a href="/olympics/friv/birthdays.cgi?month=11&amp;day=6">November 6</a>, 1987</span> in <a href="/olympics/friv/birthplaces.cgi?id=10713">Sesto Fiorentino, Firenze, Italy</a>

我正在使用这个(部分)php代码但没有成功:

foreach($html2->find($tagname_td) as $tag2) {

              if(strpos($athlete_link = $tag2->href,'athletes') == true){


                    $url_athlete='https://www.sports-reference.com' . $athlete_link;

                    $html3 = file_get_html($url_athlete,true);

              }

                           $dom = new DOMDocument();
                           $dom->loadHTML($html3);

                           $xpath = new DOMXPath($dom);
                           $div = $xpath->query('//div[@class="clear_left"]');
                           $div = $div->item(0);
                           $result = $dom->saveXML($div);
                           echo $result;

 }

我把echo $ result只用于查看它的标记,但我想要的是将这些信息存储在变量中,即:

Name = Niccolò
Surname= Campriani
Gender= male
birth = 1987-11-06
City = Sesto Fiorentino, Firenze

我该怎么办?

1 个答案:

答案 0 :(得分:0)

使用 javascript 获取一个类元素:

$('div .clear_left')

使用 jquery 获取一个类元素:

Range