getElementById:
node2:/var/www/html/aa#cat a7.html
<html>
<body>
<div id='ID'>科技</div>
<script type="text/javascript" src="jquery-2.2.2.min.js"></script>
<script type="text/javascript" src="a7.js"></script>
</body>
</html>
node2:/var/www/html/aa#cat a7.js
var domObj = document.getElementById("ID");//获得DOM对象
console.log(domObj);
var ObjHTML= domObj.innerHTML;
console.log(ObjHTML);
Navigated to http://192.168.137.3/aa/a7.html
<div id="ID">
a7.js:2:1
科技
根据ID获取值:
[root@node01 tmp]# cat a1.html
<html>
<body>
<div id='ID'>科技1</div>
<div id='ID'>科技2</div>
<script type="text/javascript" src="jquery-2.2.2.min.js"></script>
<script type="text/javascript" src="a7.js"></script>
</body>
</html>
[root@node01 tmp]# cat a1.pl
use LWP::UserAgent;
use POSIX;
use HTML::TreeBuilder::XPath;
use Encode;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;
use DBI;
use Encode;
my $tree= HTML::TreeBuilder::XPath->new;
$tree->parse_file("a1.html");
my @pageString = $tree->findvalues('//html/body/div[@id="ID"]');
print "@pageString is @pageString
";
[root@node01 tmp]# perl a1.pl
@pageString is 科技1 科技2
[root@node01 tmp]#