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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: