python request 持续更新,python爬虫

python request封装,python爬虫

请求类


import urllib.request as req
import urllib.parse as parse 
import urllib.error as uerr
import http.cookiejar as ckjar
import chardet
import requests as reqs

from urllib.parse  import urlparse 

class HttpResquestBase(object):
    """http请求类"""


    # data格式 {'key':'value'}
    data=None
    timeout=10 #默认超时时间10秒
    cookies=None # cookie
    headers=None # 请求头


    def __init__(self,url):
        self.url=url
        self.urlparse=urlparse(self.url) #分解url

    def Get(self):
        """get方法"""
        r=reqs.get(self.url,data=self.data,headers=self.headers)
        return r.text

    def urlopen(self):
        """返回html 默认get请求 添加data参数为post请求"""
        _data=None
        if(self.data!=None):
            _data=bytes(parse.urlencode(self.data),encoding="utf-8")

        resp=req.urlopen(self.url,data=_data,timeout=self.timeout)
        content=resp.read()
        result=chardet.detect(content)
        encoding=result['encoding']
        return content.decode(encoding)

    def open(self):
        """与openurl一样,可以获取cookie 执行完后可以在 cookies 中获取当前请求的cookies"""
        
        self.cookies=ckjar.CookieJar()
        handler=req.HTTPCookieProcessor(self.cookies)
        opener=req.build_opener(handler)

        _data=None
        if(self.data!=None):
            _data=bytes(parse.urlencode(self.data),encoding="utf-8")
        resp=opener.open(self.url,data=_data,timeout=self.timeout)
        content=resp.read()
        result=chardet.detect(content)
        encoding=result['encoding']
        return content.decode(encoding)

调用

url = "http://www.baidu.com";
hrb=HttpResquestBase(url)

# 添加请求头
hrb.headers={
	'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
	'Cookie':'Cookie'
}

# 发送请求
v=hrb.Get()

# 输出结果
print(v)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_42199478

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值