爬虫cookie类的封装

类的封装

from urllib import  request,parse
import json
from http import cookiejar

class save_cookie(object):
    # 创建类变量opener
    def __init__(self):
        cookie = cookiejar.CookieJar()
        handler = request.HTTPCookieProcessor(cookie)
        self.opener = request.build_opener(handler)

    def get(self, url, headers=None):
        return get(url, headers, self.opener)

    def post(self,url, data=None, headers=None):
        return post(url,data,headers,self.opener)

def get(url,headers = None,opener = None):
    return urlrequest(url,headers=headers,opener=opener)

def post(url,data = None,headers = None,opener = None):
    return urlrequest(url,data,headers,opener)

def urlrequest(url,data = None,headers = None,opener=None):

    user_agent = 'Mozilla/5.0 (Windows NTr 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
    if headers is None:
        headers = {
            'User-Agent':user_agent
        }
    result = b''
    try:
        if data:
            # POST请求
            full_data = parse.urlencode(data).encode('utf-8')
            req = request.Request(url,data=full_data,headers=headers)
        else:
            # GET请求
            req = request.Request(url,headers=headers)
        if opener:
            response = opener.open(req)
        else:
            response = request.urlopen(req)
        result = response.read()
    except Exception as e:
        print('程序内部出错,错误是:%s'%e)
    return result

if __name__ == '__main__':
    # url = 'http://www.xicidaili.com'
    # result = get(url)
    # print(result.decode('utf-8'))
    url = 'http://fanyi.baidu.com/sug'
    kw = input('请输入要翻译的词:')
    data = {
        'kw':kw
    }
    # 转json并读取有用的信息
    result = json.loads(post(url,data).decode('utf-8'))
    print(result['data'][0]['v'])

猜你喜欢

转载自blog.csdn.net/qq_41847171/article/details/81674616