Home > Backend Development > PHP Tutorial > 询问如何带cookie访问网站下载源码保存到文件

询问如何带cookie访问网站下载源码保存到文件

WBOY
Release: 2016-06-23 14:05:50
Original
1111 people have browsed it

苦苦搜寻没有结果,
请问带cookie访问个网站,读取源码,处理保存到文件或输出出来呢,新手请教不胜感激。

我自己写的,这个但是只能输出原页面效果,看不到源码。。。加了个循环却无法工作。。

<?php $url = 'http://weibo.com/***/follow'; $data = array ('foo' => 'bar'); $data = http_build_query($data); $opts = array ( 'http' => array (    'method' => 'GET',    'header'=> "Content-type: text/html" .                      "Content-Length: " . strlen($data) . "\r\n" . "cookie:***\r\n",'content' => $data ) ); $context = stream_context_create($opts); $html = file_get_contents($url,'',$context); //var_dump($html);$lines = file($html);//我估计这行可能写的不对//var_dump($lines);foreach($lines as $key => $value){   echo "{$key}:".htmlspecialchars($value)."<br>\n";}?> 
Copy after login


回复讨论(解决方案)

你 $html = file_get_contents($url,'',$context); 后
$html 中不就是文件内容了吗?
echo $html;
看一下就知道了

谢版主回复,echo $html 我试过了,浏览器刷新出来的整个页面,而不是我想要的源码效果。
就好比

<?php$url = "http://www.baidu.com/";$lines = file($url);foreach($lines as $key => $value){   echo "{$key}:".htmlspecialchars($value)."<br>\n";}?> 
Copy after login


输出的结果就是全部的源码,是想要这样的。不知道我的描述是否清楚。

自己把$html保存到一个文件,再用文本编辑器打开看看

自己把$html保存到一个文件,再用文本编辑器打开看看
你说的方法刚才试了一下是可以的,然后参照网上别人的实例,直接file_get_contents就好了,但是乱码的问题。。。

<?php $url = 'http://weibo.com/**/follow'; $data = array ('foo' => 'bar'); $data = http_build_query($data); $opts = array ( 'http' => array (    'method' => 'GET',    'header'=> "Content-type: text/html" .                      "Content-Length: " . strlen($data) . "\r\n" . "cookie:**\r\n",'content' => $data ) ); $context = stream_context_create($opts); $html = file_get_contents($url,'',$context); //$source = htmlspecialchars($html);//$code = preg_match(pattern, subject)echo htmlspecialchars($html);//var_dump($html);//$lines = file($url);//$lines = file_get_contents($url);//var_dump($lines);//$files = fopen("text.txt","w");//fwrite($files,$lines);/*foreach($html as $key => $value){   echo "{$key}:".htmlspecialchars($value)."<br>\n";}*///fclose($lines);?> 
Copy after login

乱码是因为你的编码没有统一,用header设置下字符集。
 header("content-type:text/html;charset=utf-8");

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template