# -*- 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();


输出结果:

python的http请求数据



相关文章: