我有一个问题,PHP的cURL返回一个带有一些URL的空字符串.我正在尝试解析不同网页的OG元数据,它适用于我尝试过的除NYTimes之外的所有网站.到目前为止,这是我的代码.
print_r(get_og_metadata('http://somewebsite.com'));
public function get_data($url)
{
$ch = curl_init();
$timeout = 5;
// the url to fetch
curl_setopt($ch, CURLOPT_URL, $url);
// return result as a string rather than direct output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set max time of cURL execution
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function get_og_metadata($url)
{
libxml_use_internal_errors(TRUE);
$data = $this->_get_data($url);
$doc = new DOMDocument();
$doc->loadHTML($data);
$xpath = new DOMXPath($doc);
$query = '//*/meta[starts-with(@property, \'og:\')]';
$metadatas = $xpath->query($query);
$result = array();
foreach($metadatas as $metadata) …Run Code Online (Sandbox Code Playgroud) 我想从纯PHP的外部网站获得DIV.
外部网站:http://www.isitdownrightnow.com/youtube.com.html
我想要的div文本isitdownrightnow(statusup div): <div class="statusup">The website is probably down just for you...</div>
我已经尝试过file_get_contents用DOMDocument和str_get_html,但我无法得到它的工作.
例如这个
$page = file_get_contents('http://css-tricks.com/forums/topic/jquery-selector-div-variable/');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
// Loop through the DIVs looking for one withan id of "content"
// Then echo out its contents (pardon the pun)
if ($div->getAttribute('class') === 'bbp-template-notice') {
echo $div->nodeValue;
}
}
Run Code Online (Sandbox Code Playgroud)
它只会在控制台中显示错误:
无法加载资源:服务器响应状态为500(内部服务器错误)
假设我有一个包含许多不同元素的HTML文件,每个元素都有不同的属性.假设我事先不知道这个HTML会是什么样子.
使用PHP的DOMDocument,我如何迭代所有元素并修改它们?我看到的只是getElementByTagName和getElementById等.我想遍历所有元素.
例如.让我们说HTML看起来像这样(只是一个例子,实际上我不知道结构):
$html = '<div class="potato"><span></span></div>';
Run Code Online (Sandbox Code Playgroud)
我希望能够进行一些简单的DOM修改(比如在Javascript中):
$dom = new DOMDocument();
$dom->loadHTML($html);
// Obviously the code below doesn't work but showcases what I want to achieve
foreach($dom->getAllElements as $element ){
if(!$element->hasClass('potato')){
$element->addClass('potato');
} else{
$element->removeClass('potato');
}
}
$html = $dom->SaveHTML();
Run Code Online (Sandbox Code Playgroud)
所以在这个例子中,我希望生成的html看起来像这样:
$html = '<div><span class="potato"></span></div>';
Run Code Online (Sandbox Code Playgroud)
那么如何迭代所有元素并在foreach循环中动态修改?我真的不想为此使用正则表达式.
我正在解析并将html文档提取到DOMDocument.这些文档是将在另一页内显示的子表单.在保存解析的DOMDocuments时,它会自动添加doctype,html,head和body标记.因为我正在处理子表单,我想删除所有这些并仅保存表单的子标签.
如何在保存domdocument的同时跳过自动生成html,head,body和其他标签?
我有Node一个Document.我想把Node它转换成新的根节点Document.
我能想到的唯一方法如下:
Node node = someChildNodeFromDifferentDocument;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document newDocument = builder.newDocument();
newDocument.importNode(node);
newDocument.appendChild(node);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我觉得这是相当恼人的冗长.我没有看到更简洁/更直接的方式,还是我必须这样做?
我观察到的每个浏览器都会创建一个<head>可在DOM中访问的元素,即使<head></head>文档的标记中没有显式标记也是如此.
但是,Google Analytics使用以下代码进行动态脚本插入:
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
Run Code Online (Sandbox Code Playgroud)
以下行:
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
Run Code Online (Sandbox Code Playgroud)
对于<head>没有元素的情况,似乎做出了特别的让步.
这只是一个极端向后兼容的情况(例如,对于Netscape 4等),或者有没有假设现代浏览器(即Internet Explorer 6和更新版本)始终可以访问的情况到<head>DOM中的元素?
如何通过Domdocument PHP获得第一级dom元素?
代码不起作用的示例 - 来自问答:http://stackoverflow.com/questions/1540302/how-to-get-nodes-in-first-level-using-php-domdocument
<?php
$str=<<< EOD
<div id="header">
</div>
<div id="content">
<div id="sidebar">
</div>
<div id="info">
</div>
</div>
<div id="footer">
</div>
EOD;
$doc = new DOMDocument();
$doc->loadHTML($str);
$xpath = new DOMXpath($doc);
$entries = $xpath->query("/");
foreach ($entries as $entry) {
var_dump($entry->firstChild->nodeValue);
}
?>
Run Code Online (Sandbox Code Playgroud)
谢谢你,约瑟夫
我尝试解析XML rss flux.实际上,抛出了一个错误:
Catchable fatal error: Object of class DOMElement could not be converted to string in ...
Run Code Online (Sandbox Code Playgroud)
我想获得标签"link"的值"test"
这是我的代码:
//check if url contents xml
$content = file_get_contents($flux);
$xml = new DOMDocument;
$xml->loadXML($content);
//get the link
$link = $xml->getElementsByTagName('link')->item(0);
echo $link;
Run Code Online (Sandbox Code Playgroud)
这是助焊剂:
<?xml version="1.0" encoding="ISO-8859-15" ?>
<rss version="2.0">
<channel>
<title>test</title>
<link>http://test.fr</link>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
我使用DOMDocument编辑一些HTML文件,但是一些主题在其名称空间中.所以DOMDocument自动将空格更改为%20,然后找不到它们.
这就是如何准确查看错误:
Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in file:///C:/Path/To/The/File/01%20c%2040-1964.html, line: 11 in C:/Path/To/class.php on line 51
Run Code Online (Sandbox Code Playgroud)
你知道如何修复这个错误吗?
提前感谢您的回答
我试图通过使用PHP的DOM元素和下面的HTML(相同的结构)和下面的代码来从div中获取文本,其中class = 'review-text'.
然而,这似乎不起作用
HTML
$html = '
<div class="page-wrapper">
<section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
<article class="review clearfix">
<div class="review-content">
<div class="review-text" itemprop="reviewBody">
Outstanding ...
</div>
</div>
</article>
</section>
</div>
';
Run Code Online (Sandbox Code Playgroud)PHP代码
$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if ($results->length > 0) {
echo $review = $results->item(0)->nodeValue;
}
Run Code Online (Sandbox Code Playgroud)此博客提供了按类选择元素的XPATH语法
我在StackOverflow,在线教程中尝试过很多例子,但似乎都没有.我错过了什么吗?
domdocument ×10
php ×8
html ×3
dom ×2
curl ×1
document ×1
html-parsing ×1
java ×1
javascript ×1
parsing ×1
skip ×1
xml-parsing ×1
xmlnode ×1
xpath ×1