PHP:从网站中提取 HTML 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15106456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Extract HTML data from website
提问by Saaram
I want to to extract NAME, ADDRESS and EMAIL from the website
我想从网站中提取 NAME、ADDRESS 和 EMAIL
http://agentquery.com/agent.aspx?agentid=13
http://agentquery.com/agent.aspx?agentid=13
How can I do this using file_get_contents() in PHP
我如何在 PHP 中使用 file_get_contents() 来做到这一点
For e.g
例如
$abc = file_get_content("http://agentquery.com/agent.aspx?agentid=13");
$abc = file_get_content("http://agentquery.com/agent.aspx?agentid=13");
Now how can I extract NAME, EMAIL and ADDRESS from it ?
现在如何从中提取 NAME、EMAIL 和 ADDRESS?
回答by sjdaws
This can be done with file_get_contents()and some regex processing. You must ensure you have fopen URL wrappersenabled in PHP.ini
这可以通过file_get_contents()一些正则表达式处理来完成。您必须确保在 PHP.ini 中启用了fopen URL 包装器
You need to grab the page, then find unique string to parse on. This is to get the name:
您需要抓取页面,然后找到要解析的唯一字符串。这是为了获得名称:
<?php
$page = file_get_contents('http://agentquery.com/agent.aspx?agentid=13');
// name will be inside a span ctl00_Agent1_lblName, store it in $agent_name
preg_match("/<span id=\"ctl00_Agent1_lblName\".*span>/", $page, $agent_name);
// display agent name matches
print_r($agent_name);
回答by Nirmal Ram
It's very easy just use simple html dom class and you can get the required values if you know selectors in css, jQuery
只需使用简单的 html dom 类就很容易,如果您知道 css、jQuery 中的选择器,就可以获得所需的值

