zoukankan      html  css  js  c++  java
  • scrapy中的request对象

    1. Requtes

    注意 method = 'GET'   headers = None  为字典类型   cookies = None  为字典类型

    class Request(object_ref):
    
        def __init__(self, url, callback=None, method='GET', headers=None, body=None,
                     cookies=None, meta=None, encoding='utf-8', priority=0,
                     dont_filter=False, errback=None, flags=None):
    
            self._encoding = encoding  # this one has to be set first
            self.method = str(method).upper()
            self._set_url(url)
            self._set_body(body)
            assert isinstance(priority, int), "Request priority not an integer: %r" % priority
            self.priority = priority
    
            assert callback or not errback, "Cannot use errback without a callback"
            self.callback = callback
            self.errback = errback
    
            self.cookies = cookies or {}  
            self.headers = Headers(headers or {}, encoding=encoding)
            self.dont_filter = dont_filter
    
            self._meta = dict(meta) if meta else None
            self.flags = [] if flags is None else list(flags)

    2. FormRequest

    注意: method = 'POST'  formdata 为字典类型

    class FormRequest(Request):
    
        def __init__(self, *args, **kwargs):
            formdata = kwargs.pop('formdata', None)
            if formdata and kwargs.get('method') is None:
                kwargs['method'] = 'POST'
    
            super(FormRequest, self).__init__(*args, **kwargs)  # 指的就是request
    
            if formdata:
                items = formdata.items() if isinstance(formdata, dict) else formdata
                querystr = _urlencode(items, self.encoding)
                if self.method == 'POST':
                    self.headers.setdefault(b'Content-Type', b'application/x-www-form-urlencoded')
                    self._set_body(querystr)
                else:
                    self._set_url(self.url + ('&' if '?' in self.url else '?') + querystr)
  • 相关阅读:
    vue-指令
    VueMusic-14搜索实现
    VueMusic-13歌手列表
    VueMusic-12歌词滚动
    VueMusic-11播放-歌词适配
    VueMusic-10.播放-歌词加载
    VueMusic-9.播放-播放功能
    VueMusic-8更多-下拉刷新
    VueMusic-7更多-数据适配
    VueMusic-6首页-热门榜单
  • 原文地址:https://www.cnblogs.com/yugengde/p/8027524.html
Copyright © 2011-2022 走看看