python的http请求数据

# -*- coding: utf-8 -*-
import urllib2
import urllib
'''
http请求获取页面数据
'''
class HttpOperater(object):
    #抓取页面数据
    def getPostLinkContent(self,urlStr,params):
        if(None==urlStr or urlStr==""):
            return u"url链接记住不能为空"
        ecode_param=None;
        headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'};
        if(None!=params):
            ecode_param=urllib.urlencode(params).encode('UTF-8')
            headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0","Content-Type":"application/x-www-form-urlencoded","Accept":"text/plain"}
        req = urllib2.Request(urlStr,ecode_param,headers)
        response=urllib2.urlopen(req)
        return response;

    def getGetLinkContent(self,urlStr):
        if(None==urlStr or urlStr==""):
            return u"url链接记住不能为空"
        response=urllib2.urlopen(urlStr)
        return response;
       

测试类:

#-*- coding:utf-8 -*-
import httpOperater
if __name__ == '__main__':
    param={'type':'shunfeng','postid':'6090741302**'};
    url1='http://www.kuaidi100.com/query';
    url2='http://www.kuaidi100.com/query?type=shunfeng&postid=6090741302**';
    test=httpOperater.HttpOperater();
    print u"post请求:"
    response1=test.getPostLinkContent(url1, param);
    print response1.geturl()
    print response1.getcode()
    print response1.read();
    print u"get请求:"
    response2=test.getGetLinkContent(url2);
    print response2.geturl()
    print response2.getcode()
    print response2.read();


输出结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值