Heim >Backend-Entwicklung >PHP-Tutorial >php抓取这个页面的内容

php抓取这个页面的内容

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:51:38947Durchsuche


需要抓取的部分已经用红线标出来,
只需要抓取第一页的就可以,
抓取页面:http://www.mafengwo.cn/yj/10206/2-0-1.html
一以前都是用这个类simple_html_dom.php,但是这个我用这个类抓取不出来了


回复讨论(解决方案)

可以用正?提取。

$url = "http://www.mafengwo.cn/yj/10206/2-0-1.html";
$opts = array(
  'http'=>array(
    'timeout'=>10,
    'header'=>"User-Agent: php\r\n" .
              "Cookie: foo=bar\r\n"
  )
);
$context = stream_context_create($opts);
$data = file_get_contents($url,false,$context)

这样可以读取到页面

可以用正?提取。


正则不会啊

没关系,加一个上下文(context)就可以了

include 'simple_html_dom.php';$opts = array(   'http'=>array(     'user_agent' => $_SERVER['HTTP_USER_AGENT']  ) ); $context = stream_context_create($opts); $url = 'http://www.mafengwo.cn/yj/10206/2-0-1.html';$html = file_get_html($url, false, $context);

没关系,加一个上下文(context)就可以了

include 'simple_html_dom.php';$opts = array(   'http'=>array(     'user_agent' => $_SERVER['HTTP_USER_AGENT']  ) ); $context = stream_context_create($opts); $url = 'http://www.mafengwo.cn/yj/10206/2-0-1.html';$html = file_get_html($url, false, $context);



嗯嗯,现在出来了,   输出出来的是上边的内容,但是我区分不出来了,不会用正则把信息分开了

不至于吧?

include 'simple_html_dom.php';$opts = array(   'http'=>array(     'user_agent' => $_SERVER['HTTP_USER_AGENT']  ) ); $context = stream_context_create($opts); $url = 'http://www.mafengwo.cn/yj/10206/2-0-1.html'; $html = file_get_html($url, false, $context);$div = $html->find('div.post-list ul');foreach($div[0]->find('li') as $i=>$item) {  echo $item->find('img')[0]->src, PHP_EOL;  echo trim($item->find('h2')[0]->text()), PHP_EOL;  echo trim($item->find('div')[3]->text()), PHP_EOL;  //echo '**', $item->innertext(), PHP_EOL;}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:$results = []Nächster Artikel:php 处理html时,字符串截断