cURL 学习笔记与总结(2)网页爬虫、天气预报
例1.一个简单的 curl 获取百度 html 的爬虫程序(crawler):
spider.php
<?php
/*
获取百度html的简单网页爬虫
*/
$curl = curl_init('http://www.baidu.com'); //resource(2, curl)
curl_exec($curl);
curl_close($curl);
访问该页面:

例2.下载一个网页(百度)并把内容中的百度替换成'PHP'之后输出
<?php
/*
下载一个网页(百度)并把内容中的百度替换成'PHP'之后输出
*/
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.baidu.com'); //设置访问网页的url
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); //执行之后不直接打印出来
$output = curl_exec($curl); //执行
curl_close($curl); //关闭cURL
echo str_replace('百度','PHP',$output);
访问该页面:

例3.调用 WenService 获取天气信息
WeatherWS(http://www.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather) 提供了多种方式获取天气信息,例如 soap1.1,soap1.2,http get,http post。
现在使用 curl 模拟 http post 来获取天气数据。

weather.php
<?php
/*
cURL调用WebService查询北京的当前天气
*/
$data = 'theCityName=北京';
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName');
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_POST,1); //post方式
curl_setopt($curl,CURLOPT_POSTFIELDS,$data); //设置post的参数
curl_setopt($curl,CURLOPT_HTTPHEADER,array('application/x-www-form-urlencoded;charset=utf-8','Content-length: '.strlen($data)));
curl_setopt($curl, CURLOPT_USERAGENT, "user-agent:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0"); //解决错误:“未将对象引用设置到对象的实例。”
$rtn = curl_exec($curl);
if(!curl_errno($curl)){
//$info = curl_getinfo($curl);
//print_r($info);
echo $rtn;
}else{
echo 'curl error: '.curl_error($curl);
}
curl_close($curl);
页面输出:

也可以在 cmd 中使用:
C:\Users\Administrator>d: D:\>cd practise/php/curl D:\practise\php\curl>php -f weather.php > weather.txt
把返回的结果保存在 txt 文件中(如果报错,参考 php运行出现Call to undefined function curl_init()的解决方法)。
cURL 学习笔记与总结(2)网页爬虫、天气预报的更多相关文章
- cURL 学习笔记与总结(1)概念
概念: cURL(Client URL Library Functions)is a command line tool for transfering data with URL syntax(使用 ...
- python学习笔记--Django入门一 网页显示时间
我的笔记是学习http://djangobook.py3k.cn/ 课程时做的,这个上边的文章讲的确实是非常的详细,非常感谢你们提供的知识. 上一篇随笔中已经配置好了Django环境,现在继续跟随ht ...
- cURL 学习笔记与总结(5)用 cURL 访问 HTTPS 资源
<?php $curlobj = curl_init(); // 初始化 curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnet ...
- Python学习笔记之爬取网页保存到本地文件
爬虫的操作步骤: 爬虫三步走 爬虫第一步:使用requests获得数据: (request库需要提前安装,通过pip方式,参考之前的博文) 1.导入requests 2.使用requests.get ...
- cURL 学习笔记与总结(4)使用 cURL 从 ftp 上下载文件与上传文件到 ftp
下载: <?php $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, "ftp://192.***.*.***/文件 ...
- Python3学习笔记2:简易Web爬虫
开发环境 基础语法那章的内容我是在Docker容器中玩的,但是真正做项目的时候,没有IDE的强大辅助功能来协助的话是很累人的一件事.因此从本文中,我选择使用Jetbrain的Pycharm这个IDE来 ...
- Html+css学习笔记一 创建一个网页
第一个网页 新建一个记事本,把名字改成first.html <html> <head> <title>MyFristHtml</title> </ ...
- Android学习笔记_69_android 支付宝之网页支付和快捷支付
参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...
- 爬虫制作入门学习笔记2:[转]python爬虫实例项目大全
WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...
随机推荐
- MySQL auto-extending data file
http://blog.csdn.net/hw_libo/article/details/39215723 http://blog.sina.com.cn/s/blog_5037eacb0102vjm ...
- [图文详解] Sublime Text在Windows/Ubuntu/Mac OSX中配置使用CTags
很开发者都在找Sublime Text中函数转跳的功能,这个是软件自身没有的功能,要靠CTags这个插件配合CTags的可执行程序的实现的.按照我的理解是CTags扫描索引你的项目文件,然后subli ...
- [hive小技巧]同一份数据多种处理
其实就是from表时,可以插入到多个表. sql语句的模板如下: from history insert overwrite sales select * where actino='purchase ...
- hdu 1754 Ihate it
I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- python 自定义排序函数
自定义排序函数 Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 ...
- hdu 1284 分硬币 && uva 147
#include<bits/stdc++.h> using namespace std; int main() { unsigned ]; memset(dp,,sizeof(dp)); ...
- WPF/Silverlight HierarchicalDataTemplate 模版的使用(转)
上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...
- 2016 Multi-University Training Contest 10
solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...
- 【python游戏编程之旅】第二篇--pygame中的IO、数据
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 在上一篇中,我们介绍了pygame的入门操作http://www.cnblogs.com/msxh/p/49 ...
- 打包apk
apk 配置环境变量 打开"终端",输入"pico .bash_profile"命令 export ANDROID_SDK_ROOT=/Users/sun/Do ...