用PHP从craigslist检索帖子
我想使用用PHP从craigslist检索帖子,php,dom,file-get-contents,Php,Dom,File Get Contents,我想使用file\u get\u contents只获取p 标签 我曾尝试使用DOM元素,但当我使用getElementByTagName并循环遍历数组时,它会剥离标记中的所有HTML <?php function search(){ $city = $_POST['city']; $scity = trim($city); $dom = new DOMDocument; libxml_use_internal_errors(true); $dom
file\u get\u contents
只获取p
标签
我曾尝试使用DOM元素,但当我使用getElementByTagName
并循环遍历数组时,它会剥离
标记中的所有HTML
<?php
function search(){
$city = $_POST['city'];
$scity = trim($city);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://'.$scity.'.craigslist.org/search/cto?query=Toyota');
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$contents = $xpath->query('//p[@class="row"]');
foreach($contents as $val){
echo '<a href="' . $val->getElementsByTagName('a')->getAttribute('href') . '">' . utf8_decode(trim($val->nodeValue, " -,")) . "</a><br />\n";;
}
}
?>
使用preg\u match\u all()
:
$text=文件获取内容(“…”);
preg|u match|u all(“|(.*)|,$texst,$matches)//使用正则表达式匹配所有内部变量`p`
var_dump($matches)//观看比赛
Craigslist对刮刀采取强硬态度。如果你还没有被阻止,你会在你一受欢迎的时候被阻止。请发布你的代码。还有一些示例HTML,以及您想从中提取的内容。@MichaelPetrotta我刚刚更新了这篇文章。
$text = file_get_contents("....");
preg_match_all('|<p>(.*?)</p>|',$texst, $matches); //Use regex to match all inside `p`
var_dump($matches); //View the matches