现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。
分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
* 实例描述:模拟需要登录的网址并抓取网页的内容
*/
$data=array(‘username’ => ‘promonkey’,
‘password’ => ‘1q2w3e’,
‘remember’=>1);
$data=‘username=zjzhoufy@126.com&password=1q2w3e&remember=1’;
c
u
r
l
o
b
j
=
c
u
r
l
_
i
n
i
t
(
)
;
/
/
初始化
c
u
r
l
_
s
e
t
o
p
t
(
curlobj = curl\_init(); // 初始化 curl\_setopt(
curlobj=curl_init();//初始化curl_setopt(curlobj, CURLOPT_URL, “http://www.imooc.com/user/login”); // 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来
// Cookie相关设置,这部分设置需要在所有会话开始之前设置
date_default_timezone_set(‘PRC’); // 使用Cookie时,必须先设置时区
curl_setopt(
c
u
r
l
o
b
j
,
C
U
R
L
O
P
T
_
C
O
O
K
I
E
S
E
S
S
I
O
N
,
T
R
U
E
)
;
c
u
r
l
_
s
e
t
o
p
t
(
curlobj, CURLOPT\_COOKIESESSION, TRUE); curl\_setopt(
curlobj,CURLOPT_COOKIESESSION,TRUE);curl_setopt(curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); // 这样能够让cURL支持页面链接跳转
curl_setopt(
c
u
r
l
o
b
j
,
C
U
R
L
O
P
T
_
P
O
S
T
,
1
)
;
c
u
r
l
_
s
e
t
o
p
t
(
curlobj, CURLOPT\_POST, 1); curl\_setopt(
curlobj,CURLOPT_POST,1);curl_setopt(curlobj, CURLOPT_POSTFIELDS,
d
a
t
a
)
;
c
u
r
l
_
s
e
t
o
p
t
(
data); curl\_setopt(
data);curl_setopt(curlobj, CURLOPT_HTTPHEADER, array(“application/x-www-form-urlencoded; charset=utf-8”,
"Content-length: ".strlen(
d
a
t
a
)
)
)
;
c
u
r
l
_
e
x
e
c
(
data) )); curl\_exec(
data)));curl_exec(curlobj); // 执行
curl_setopt(
c
u
r
l
o
b
j
,
C
U
R
L
O
P
T
_
U
R
L
,
"
h
t
t
p
:
/
/
w
w
w
.
i
m
o
o
c
.
c
o
m
/
s
p
a
c
e
/
i
n
d
e
x
"
)
;
c
u
r
l
_
s
e
t
o
p
t
(
curlobj, CURLOPT\_URL, "http://www.imooc.com/space/index"); curl\_setopt(
curlobj,CURLOPT_URL,"http://www.imooc.com/space/index");curl_setopt(curlobj, CURLOPT_POST, 0);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array(“Content-type: text/xml”
));
o
u
t
p
u
t
=
c
u
r
l
_
e
x
e
c
(
output=curl\_exec(
output=curl_exec(curlobj); // 执行
curl_close($curlobj); // 关闭cURL
echo $output;
?>
**4. 登录网站的信息抓取并下载个人空间页面 + 自定义实现页面链接跳转抓取**
<?php /\*\* \* 实例描述:登录网站的信息抓取并下载个人空间页面 \* 自定义实现页面链接跳转抓取 \* \*/ $data='username=demo\_peter@126.com&password=123qwe&remember=1'; $curlobj = curl\_init(); // 初始化 curl\_setopt($curlobj, CURLOPT\_URL, "http://www.imooc.com/user/login"); // 设置访问网页的URL curl\_setopt($curlobj, CURLOPT\_RETURNTRANSFER, true); // 执行之后不直接打印出来 // Cookie相关设置,这部分设置需要在所有会话开始之前设置 date\_default\_timezone\_set('PRC'); // 使用Cookie时,必须先设置时区 curl\_setopt($curlobj, CURLOPT\_COOKIESESSION, TRUE); curl\_setopt($curlobj, CURLOPT\_HEADER, 0); // 注释掉这行,因为这个设置必须关闭安全模式 以及关闭open\_basedir,对服务器安全不利 //curl\_setopt($curlobj, CURLOPT\_FOLLOWLOCATION, 1); curl\_setopt($curlobj, CURLOPT\_POST, 1); curl\_setopt($curlobj, CURLOPT\_POSTFIELDS, $data); curl\_setopt($curlobj, CURLOPT\_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data) )); curl\_exec($curlobj); // 执行 curl\_setopt($curlobj, CURLOPT\_URL, "http://www.imooc.com/space/index"); curl\_setopt($curlobj, CURLOPT\_POST, 0); curl\_setopt($curlobj, CURLOPT\_HTTPHEADER, array("Content-type: text/xml" )); $output=curl\_redir\_exec($curlobj); // 执行 curl\_close($curlobj); // 关闭cURL echo $output; /\*\* \* 自定义实现页面链接跳转抓取 \*/ function curl\_redir\_exec($ch,$debug="") { static $curl\_loops = 0; static $curl\_max\_loops = 20; if ($curl\_loops++ >= $curl\_max\_loops) { $curl\_loops = 0; return FALSE; } curl\_setopt($ch, CURLOPT\_HEADER, true); // 开启header才能够抓取到重定向到的新URL curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, true); $data = curl\_exec($ch); // 分割返回的内容 $h\_len = curl\_getinfo($ch, CURLINFO\_HEADER\_SIZE); $header = substr($data,0,$h\_len); $data = substr($data,$h\_len - 1); $http\_code = curl\_getinfo($ch, CURLINFO\_HTTP\_CODE); if ($http\_code == 301 || $http\_code == 302) { $matches = array(); preg\_match('/Location:(.\*?)\n/', $header, $matches); $url = @parse\_url(trim(array\_pop($matches))); // print\_r($url); if (!$url) { //couldn't process the url to redirect to $curl\_loops = 0; return $data; } $last\_url = parse\_url(curl\_getinfo($ch, CURLINFO\_EFFECTIVE\_URL)); if (!isset($url['scheme'])) $url['scheme'] = $last\_url['scheme']; if (!isset($url['host'])) $url['host'] = $last\_url['host']; if (!isset($url['path'])) $url['path'] = $last\_url['path']; $new\_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query'])?'?'.$url['query']:''); curl\_setopt($ch, CURLOPT\_URL, $new\_url); return curl\_redir\_exec($ch); } else { $curl\_loops=0; return $data; } } ?>
**从FTP服务器下载一个文件到本地**
<?php /\*\* \* 实例描述:把本地文件上传到FTP服务器上 \*/ $curlobj = curl\_init(); $localfile = 'ftp01.php'; $fp = fopen($localfile, 'r'); curl\_setopt($curlobj, CURLOPT\_URL, "ftp://192.168.1.100/ftp01\_uploaded.php"); curl\_setopt($curlobj, CURLOPT\_HEADER, 0); curl\_setopt($curlobj, CURLOPT\_RETURNTRANSFER, 1); curl\_setopt($curlobj, CURLOPT\_TIMEOUT, 300); // times out after 300s curl\_setopt($curlobj, CURLOPT\_USERPWD, "peter.zhou:123456");//FTP用户名:密码 //上传和下载主要是下面子三个参数不一样 curl\_setopt($curlobj, CURLOPT\_UPLOAD, 1); curl\_setopt($curlobj, CURLOPT\_INFILE, $fp); curl\_setopt($curlobj, CURLOPT\_INFILESIZE, filesize($localfile)); $rtn = curl\_exec($curlobj); fclose($fp); if(!curl\_errno($curlobj)){ echo "Uploaded successfully."; } else { echo 'Curl error: ' . curl\_error($curlobj); } curl\_close($curlobj); ?>
**6.下载网络上面的一个HTTPS的资源**
<?php /\*\* \* 实例描述:下载网络上面的一个HTTPS的资源 \*/ $curlobj = curl\_init(); // 初始化 curl\_setopt($curlobj, CURLOPT\_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js"); // 设置访问网页的URL curl\_setopt($curlobj, CURLOPT\_RETURNTRANSFER, true); // 执行之后不直接打印出来 // 设置HTTPS支持 date\_default\_timezone\_set('PRC'); // 使用Cookie时,必须先设置时区 curl\_setopt($curlobj, CURLOPT\_SSL\_VERIFYPEER, 0); // 对认证证书来源的检查从证书中检查SSL加密算法是否存在 curl\_setopt($curlobj, CURLOPT\_SSL\_VERIFYHOST, 2); // $output=curl\_exec($curlobj); // 执行 curl\_close($curlobj); // 关闭cURL echo $output; ?>
### 原生PHP模拟http请求
有时候为了简单模拟一个http请求,这样就去用curl有点浪费,其实php自身已经可以实现这个功能,
需要在服务器端模拟 POST/GET 等请求,也就是在 PHP 程序中去实现模拟,改怎么做到呢?或者说,在 PHP 程序里,给你一个数组,如何将这个数组 POST/GET 到另外一个地址呢?当然,使用 CURL 很容易办到,那么如果不使用 CURL 库,又该怎么办呢?其实,在 PHP 里已经有相关的函数实现了,这个函数就是接下来要讲的 `stream_context_create()。`
直接 show you the code,这是最好的方法:
$data = array(
‘foo’=>‘bar’,
‘baz’=>‘boom’,
‘site’=>‘www.nowamagic.net’,
‘name’=>‘nowa magic’);
d a t a = h t t p b u i l d q u e r y ( data = http_build_query( data=httpbuildquery(data);
//
p
o
s
t
d
a
t
a
=
h
t
t
p
_
b
u
i
l
d
_
q
u
e
r
y
(
postdata = http\_build\_query(
postdata=http_build_query(data);
$options = array(
‘http’ => array(
‘method’ => ‘POST’,
‘header’ => ‘Content-type:application/x-www-form-urlencoded’,
‘content’ => $data
//‘timeout’ => 60 * 60 // 超时时间(单位:s)
)
);
$url = “http://www.nowamagic.net/test2.php”;
c
o
n
t
e
x
t
=
s
t
r
e
a
m
c
o
n
t
e
x
t
c
r
e
a
t
e
(
context = stream_context_create(
context=streamcontextcreate(options);
r
e
s
u
l
t
=
f
i
l
e
g
e
t
c
o
n
t
e
n
t
s
(
result = file_get_contents(
result=filegetcontents(url, false, $context);
echo $result;
http://www.nowamagic.net/test2.php 的代码为:
$data = $_POST;
echo ‘
’;’;
print_r( $data );
echo ‘
运行结果为:
Array
(
[foo] => bar
[baz] => boom
[site] => www.nowamagic.net
[name] => nowa magic
)
一些要点讲解:
1. 以上程序用到了 `http_build_query()` 函数,构造URL字符串。
2. `stream_context_create()` 是用来创建打开文件的上下文件选项的,比如用POST访问,使用代理,发送header等。就是 创建一个流,再举一个例子吧:
c
o
n
t
e
x
t
=
s
t
r
e
a
m
c
o
n
t
e
x
t
c
r
e
a
t
e
(
a
r
r
a
y
(
′
h
t
t
p
′
=
>
a
r
r
a
y
(
′
m
e
t
h
o
d
′
=
>
′
P
O
S
T
′
,
′
h
e
a
d
e
r
′
=
>
s
p
r
i
n
t
f
(
"
A
u
t
h
o
r
i
z
a
t
i
o
n
:
B
a
s
i
c
context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => sprintf("Authorization: Basic %s\r\n", base64_encode(
context=streamcontextcreate(array(′http′=>array(′method′=>′POST′,′header′=>sprintf("Authorization:Basicusername.‘:’.$password)).
“Content-type: application/x-www-form-urlencoded\r\n”,
‘content’ => http_build_query(array(‘status’ => $message)),
‘timeout’ => 5,
),
));
$ret = file_get_contents(‘http://twitter.com/statuses/update.xml’, false, $context);
1. `stream_context_create`创建的上下文选项即可用于流(stream),也可用于文件系统(file system)。对于像 `file_get_contents`、`file_put_contents`、`readfile`直接使用文件名操作而没有文件句柄的函数来说更有用。`stream_context_create`增加header头只是一部份功能,还可以定义代理、超时等。这使得访问web的功能不弱于curl。
2. `stream_context_create()` 作用:创建并返回一个文本数据流并应用各种选项,可用于`fopen()`,`file_get_contents()`等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
3. `stream_context_create` 还能通过增加 timeout 选项解决`file_get_contents`超时处理:
$opts = array(
‘http’=>array(
‘method’=>“GET”,
‘timeout’=>60,
)
);
//创建数据流上下文
最后
Python崛起并且风靡,因为优点多、应用领域广、被大牛们认可。学习 Python 门槛很低,但它的晋级路线很多,通过它你能进入机器学习、数据挖掘、大数据,CS等更加高级的领域。Python可以做网络应用,可以做科学计算,数据分析,可以做网络爬虫,可以做机器学习、自然语言处理、可以写游戏、可以做桌面应用…Python可以做的很多,你需要学好基础,再选择明确的方向。这里给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
👉Python所有方向的学习路线👈
Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
👉Python必备开发工具👈
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
👉Python全套学习视频👈
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
👉实战案例👈
学python就与学数学一样,是不能只看书不做题的,直接看步骤和答案会让人误以为自己全都掌握了,但是碰到生题的时候还是会一筹莫展。
因此在学习python的过程中一定要记得多动手写代码,教程只需要看一两遍即可。
👉大厂面试真题👈
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!