前段时间提取了一个工具类,分享给大家:

<?php
class httpconnector {
private $curl;
private $cookie;
private $kv;
function __construct(){
$this->kv = new SaeKV();
$this->kv->init();
if($data=$this->kv->get("cookie"))
$this->cookie=$data; }
public function get($url) {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($this->curl);
curl_close($this->curl);
preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);
foreach ($match as $r) {
if ($this->cookie != '') {
$this->cookie = $this->cookie . ';';
}
if (isset($r[1])) {
$this->cookie .= trim(str_replace("\r\n", "", $r[1]));
}
}
$this->kv->set("cookie",$this->cookie);
return $data; }
public function post($url, $params) {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($this->curl);
curl_close($this->curl);
preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);
foreach ($match as $r) {
if ($this->cookie != '') {
$this->cookie = $this->cookie . ';';
}
if (isset($r[1])) {
$this->cookie .= trim(str_replace("\r\n", "", $r[1]));
}
}
$this->kv->set("cookie",$this->cookie);
return $data; }
}
?>

一个网页抓取的类支持get+post+cookie存储的更多相关文章

  1. 分享一个c#t的网页抓取类

    using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...

  2. Java实现网页抓取的一个Demo

    这个小案例的话我是存放在我的github 上. 下面给出链接自己可以去看下,也可以直接下载源码.有具体的说明 <Java网页抓取>

  3. 基于Casperjs的网页抓取技术【抓取豆瓣信息网络爬虫实战示例】

    CasperJS is a navigation scripting & testing utility for the PhantomJS (WebKit) and SlimerJS (Ge ...

  4. Python开发爬虫之动态网页抓取篇:爬取博客评论数据——通过Selenium模拟浏览器抓取

    区别于上篇动态网页抓取,这里介绍另一种方法,即使用浏览器渲染引擎.直接用浏览器在显示网页时解析 HTML.应用 CSS 样式并执行 JavaScript 的语句. 这个方法在爬虫过程中会打开一个浏览器 ...

  5. Python爬虫之三种网页抓取方法性能比较

    下面我们将介绍三种抓取网页数据的方法,首先是正则表达式,然后是流行的 BeautifulSoup 模块,最后是强大的 lxml 模块. 1. 正则表达式   如果你对正则表达式还不熟悉,或是需要一些提 ...

  6. Python之HTML的解析(网页抓取一)

    http://blog.csdn.net/my2010sam/article/details/14526223 --------------------- 对html的解析是网页抓取的基础,分析抓取的 ...

  7. java网页抓取

    网页抓取就是,我们想要从别人的网站上得到我们想要的,也算是窃取了,有的网站就对这个网页抓取就做了限制,比如百度 直接进入正题 //要抓取的网页地址 String urlStr = "http ...

  8. 网页抓取:PHP实现网页爬虫方式小结

    来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...

  9. Python实现简单的网页抓取

    现在开源的网页抓取程序有很多,各种语言应有尽有. 这里分享一下Python从零开始的网页抓取过程 第一步:安装Python 点击下载适合的版本https://www.python.org/ 我这里选择 ...

随机推荐

  1. [Outlook] outlook如何实现自动CC和BCC邮件发送

    由于需要在不同机器上发送邮件,最终发送的邮件会在不同的机器上,最终导致邮件丢失,以后想找也找不回来,故在网上搜索一翻,找到解决办法. 1. 实现自动CC邮件发送: 方法:使用outlook的配置规则 ...

  2. PMP 第八章 项目质量管理

    1规划质量 2实施质量保证 3实施质量控制 质量成本 1.等级和质量的区别?现代质量管理的重要性,关注图8-2    质量是一些列内在特性满足要求的程度,而等级是对用途相同但技术特性不同的产品或服务的 ...

  3. 禁用编译器自动生成的函数(Effective C++之06)

    如果想让你的类定义出来的对象是独一无二的,即对象无法被复制,或者使用赋值操作符赋给另外一个对象,那么最好的方法就是禁用拷贝构造函数和赋值操作符.下面介绍几种禁用的方法.(方法来自Effective C ...

  4. Intent传递类实例

    发送方: Intent intent = new Intent(); intent.setClass(mContext, HomeDetailReportActivity.class); intent ...

  5. Java学习随笔1:Java是值传递还是引用传递?

    Java always passes arguments by value NOT by reference. Let me explain this through an example: publ ...

  6. SoapUI接口测试之实战运用操作(五)

    SoapUI接口测试之实战运用操作(五)

  7. 《DSP using MATLAB》为什么要z变换?

    书中截图

  8. coffeeScript学习02

    闭包 closure = do -> _private = "foo" -> _private console.log(closure()) #=> " ...

  9. Something about "if"

    分支:If引导了一个分支,语法结构有{if:if, else if,else:if,else:等}if可以单独使用,不一定非要加else,但是为了语句的严谨或者程序的稳定,请尽量使用else(注意if ...

  10. JS实现选择不同select标签option值的验证

    js实现不同select标签option值的验证 功能描述: 选择中文时,匹配中文的正则表达式,选择英文选项是匹配英文的表达式,并且有对应的提示信息. html代码片段: <select id= ...