duansai1314 2015-03-12 07:23
浏览 170
已采纳

xpath得到div id无法正常工作

my script dont get any results(blank page) and i dont understand why I've used this script on other websites and work

$url = 'http://ip-score.com/checkip/207.224.78.193';

libxml_use_internal_errors(true);
$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$my_xpath_query = "//div[@id='MaxMind']";
$result_rows = $xpath->query($my_xpath_query);

foreach ($result_rows as $result_object){
echo $result_object->childNodes->item(0)->nodeValue;
}

i've aspect to get this results:

207.224.78.193  United States
State: Minnesota
City: Minneapolis
ZIP: 55407
Hostname: 207-224-78-193.mpls.qwest.net, Whois
Organization: CenturyLink, History
ISP: CenturyLink
Mail Server: N\A
IP range: 207.224.71.0 - 207.224.97.191
Current region time: Thu 12th Mar 2015 - 02:52:15 GMT: -05:00
Region timezone: America/Chicago

i want to get all info from url(state,city,zip...) but i can not get it one by one so i decise to get all then split it into var

  • 写回答

1条回答 默认 最新

  • dongmanni6916 2015-03-12 07:36
    关注

    Your xpath expression is currently looking for that div as the root element of the document. You probably want to use //div[@id='MaxMind'] to search the document entirely, or just use $dom->getElementById("MaxMind") as xpath isn't really needed for this query.

    Also worth noting: the first child of that element is just a text node full of white space. To dump out everything in the div replace

    echo $result_object->childNodes->item(0)->nodeValue;
    

    with

    echo $result_object->nodeValue;
    

    Though if you want to get at that stuff in a more reasonable manner, instead of just one blob of text, an xpath expression like //div[@id='MaxMind']/p will give you each "line" within.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题