如何从html页面捕获信息

时间:2018-02-19 12:09:47

标签: php html

<h1 class="float_left margin_right margin_bottom">Emilio Banfi</h1>
    <div class="clear_left"></div>
    <p><span class="bold_text">Full name:</span> Emilio Banfi
    <br><span class="bold_text">Gender:</span> Male
    <br><span class="bold_text">Born:</span> 1881 in <a href="/olympics/friv/birthplaces.cgi?id=10696">Saronno, Varese, Italy</a>

如何使用php获取以下两个信息(我将它们放在变量中):

1881
Saronno, Varese

2 个答案:

答案 0 :(得分:0)

如果您只想使用php,请尝试将class添加到

 <br><span class="bold_text">Born:</span> 
<span class="type">1881</span> in <a href="/olympics/friv/birthplaces.cgi?id=10696"><span class="type">Saronno</span>, 
<span class="type">Varese</span>, Italy</a>

并且

$doc = new DomDocument();
$doc->loadHTMLFile('www.youpage.com');
$thediv = $doc->getElementByClass('type');
echo $thediv->textContent;

但更好的选择是使用js

答案 1 :(得分:0)

HTML

<br><span class="bold_text">Born:</span><span id="get_age"> 1881 </apan>in <a href="/olympics/friv/birthplaces.cgi?id=10696"><span id="get_name">Saronno, Varese, Italy</apan></a>

AJAX

getRecord();
function getRecord()
{
            var age=jQuery('#get_age').text();
            var name=jQuery('#get_name').text();
            jQuery.ajax({
                type: "POST",
                url: 'action.php',
                data:{age:age,name:name},
                async: false,
                success: function (data)
                {
                    alert('success');
                }
            });
}

PHP action.php的

echo "<pre>";print_r($_POST);