0 php
我在php 7.1上运行简单的html dom.
但是第一行我无法解析html
<?php
include 'simple_html_dom.php';
$html = file_get_html('http://google.com');
echo $html;
?>
Run Code Online (Sandbox Code Playgroud)
该页面不显示任何内容(白色背景)和上述代码.
但是下面的代码却运行:
<?php
include 'simple_html_dom.php';
//base url
$base = 'https://google.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
// Create a DOM object
$html_base = new simple_html_dom();
// Load HTML from a string
$html_base->load($str);
echo $html_base;
$html_base->clear();
unset($html_base);
?>
Run Code Online (Sandbox Code Playgroud)
然后,我尝试使用以上代码获取img类代码,但没有工作:
获取图片html:
<div class="product_thumb">
<a title="Me Before You" class="image-border" href=/me-before-you-a-novel-movie-tie-in-p69988.html">
<img class=" pict lazy-img" id="det_img_00069988"
src="/images/thumbnails/product/115x/222614_me-before-you-a-novel-movie-tie-
in.jpg">
</a></div>
Run Code Online (Sandbox Code Playgroud)
我的简单HTML DOM,所有都不工作(在页面上没有html)
//* Find all images 1st code
foreach($html_base->find('img[class= pict lazy-img]') as $element)
echo '<img src="' . $element->src . '" />' . '<br>';
//* Find all images 2nd code
foreach($html_base->find('img[class= pict lazy-img]',0) as $element)
echo '<img src="' . $element->src . '" />' . '<br>';
//* Find all images 3rd code
foreach($html_base->find('img[class$=pict lazy-img]',0) as $element)
echo '<img src="' . $element->src . '" />' . '<br>';
//* Find all images 4th code
foreach($html_base->find('img[class$=pict lazy-img]',0) as $element)
echo '<img src="' . $element->src . '" />' . '<br>';
Run Code Online (Sandbox Code Playgroud)
小智 6
file_get_htmlsimple_html_dom需要更改包含文件中的更改.见下文,它对我有用.请参阅链接https://sourceforge.net/p/simplehtmldom/bugs/161/
从PHP 7.1开始,可以解释负偏移.默认值offset必须从更改-1为0.
function file_get_html($url, $use_include_path = false, $context=null, $offset = 0, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4766 次 |
| 最近记录: |