GET和POST的封装

from urllib import request, parse
from urllib.error import HTTPError, URLError

#我们这里将昨天的进行完善,加上Cookie

from http import cookiejar
class session(object):

  def __init__(self):
    #保存cookie
    cookie_object = cookiejar.CookieJar()
    handler = request.HTTPCookieProcessor(cookie_object)
    self.opener = request.build_opener(handler)
  
  def get(self, url, headers=None):
    return get(url, headers, self.opener)
  
  def post(self, url, form, headers=None):
    return post(ur;, form, headers, self.opener)
    
  
def
get(url, headers=None, opener=None): return urlrequests(url, headers = headers, opener = opener) def post(url,form, headers=None, opener = None): return urlrequests(url, form, headers=headers, opener = opener) def urlrequests(url, form=None,headers=None, opener=None): user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36' #如果用户需要自行传入

  if headers == None:
    headers = {
      'User-Agent': user_agent
    }
  
  html_bytes = b''
  
  try:
    if form:
      #POST
      #2.1转换成str
      form_str = parse.urlencode(form)
      #2.2转换成butes
      form_bytes = form_str.encode('utf-8')
      req = request.Request(url, data=form_bytes, headers+headers)
    else:
      #GET
      req = request.Request(url, headers=headers)
    if opener:
      response = opener.open(req)
    else:
      response = request.urlopen(req)
  
    html_byres = response.read()
  except HTTPError as e:
    print(e)
  except URLError as e:
    print(e)
  return html_bytes

if __name__ == '__main__':
  url = 'http://fanyi.baidu.com/sug'
  form = {
    'kw': '呵呵'
  }
  html_bytes = post(url, form=form)
  print(html_bytes)

  #url = 'http://www.baidu.com'
  #html_byte = get(url)
  #print(html_byte)
  
        

 

转载于:https://www.cnblogs.com/gxsmm/p/9471586.html

你可能感兴趣的:(GET和POST的封装)