Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 得名_Php_Html - Fatal编程技术网

Php 得名

Php 得名,php,html,Php,Html,我正在尝试使用simple_html_dom获取一篇文章的标题可以在下面看到html根,我正在尝试获取的部分是标题这是我们的标题 最新消息 新增日期:2015年12月16日 现在我有这个 $page = (isset($_GET['p'])&&$_GET['p']!=0) ? (int) $_GET['p'] : ''; $html = file_get_html('http://www.example.com/'.$page); foreach($html-

我正在尝试使用simple_html_dom获取一篇文章的标题可以在下面看到html根,我正在尝试获取的部分是标题这是我们的标题



最新消息

  • 新增日期:2015年12月16日
现在我有这个

$page = (isset($_GET['p'])&&$_GET['p']!=0) ? (int) $_GET['p'] : '';

$html = file_get_html('http://www.example.com/'.$page);

foreach($html->find('div#section ul.cont li div a') as $element)
{
    print '<br><br>';
    echo $url = 'http://www.example.com/'.$element->href;

    $html2 = file_get_html($url);

    print '<br>';

    $image = $html2->find('meta[property=og:image]',0);
    print $image = $image->content;

    print '<br>';

    $title = $html2->find('#sectionleft ul.cont news li a.name',0);
    print $title = $title->plaintext;

    print '<br>';
}
$page=(isset($\u GET['p'])&&&$\u GET['p']!=0)?(int)$_GET['p']:'';
$html=file\u get\u html('http://www.example.com/":$页),;
foreach($html->find('div#section ul.cont li div a')作为$element)
{
打印“

”; echo$url=http://www.example.com/'.$element->href; $html2=文件\获取\ html($url); 打印“
”; $image=$html2->find('meta[property=og:image]',0); 打印$image=$image->content; 打印“
”; $title=$html2->find(“#sectionleft ul.cont news li a.name”,0); 打印$title=$title->纯文本; 打印“
”; }

问题是这里的
$title=$html2->find('sectionleft ul.cont news li a.name',0)我假设我使用了错误的选择器,但我确实不确定我做错了什么

ul.cont新闻
表示“查找
属于
ul.cont
的子元素”

你实际上想要:

#sectionleft ul.cont.news li a.name
编辑:出于某种原因,它似乎不喜欢
ul.cont.news
,尽管它是一个有效的CSS选择器

你可以试试

#sectionleft ul[class="cont news"] li a.name
只要类的顺序是那样的,就可以了。

如果这看起来有点老套,请原谅,但是。。。您始终可以使用PHP运行快速的
.js

<?php

echo '<script>';
echo 'var postTitle = document.querySelector("ul.cont.news a.name").innerHTML;';
if (!isset($_GET['posttitle'])) {
echo 'window.location.href = window.location.href + "?posttitle=" + postTitle';}
echo '</script>';

$postTitle = $_GET['posttitle'];

?>

@Open我想你忘了div:
$title=$html2->find('div#sectionleft ul.cont\.news li a.name',0)@dstudeba:那没关系
sectionleft
是一个ID;它在整个页面中都是独一无二的。@dstudeba也不起作用,只是显示一个空白,而不是显示我们要查找的标题。@RocketHazmat谢谢,当我将OP中的代码片段更改为您在此处所说的
$title=$html2->find('div#sectionleft ul.cont.news li a.name',0)时,我应该返回到BeautifulSoup没有任何变化,但进一步研究后,它应该。。
<?php

echo '<script>';
echo 'var postTitle = document.querySelector("ul.cont.news a.name").innerHTML;';
if (!isset($_GET['posttitle'])) {
echo 'window.location.href = window.location.href + "?posttitle=" + postTitle';}
echo '</script>';

$postTitle = $_GET['posttitle'];

?>