地狱怪客

scrapy爬虫添加cookie

class transCookie:
    def __init__(self, cookie):
        self.cookie = cookie

    def stringToDict(self):
        '''
        将从浏览器上Copy来的cookie字符串转化为Scrapy能使用的Dict
        :return:
        '''
        itemDict = {}
        items = self.cookie.split(';')
        for item in items:
            key = item.split('=')[0].replace(' ', '')
            value = item.split('=')[1]
            itemDict[key] = value
        return itemDict

if __name__ == "__main__":
    cookie = 'wordpress_logged_in_d3bee739xxxxe895=xxxx|1529479111|gwhpBEo2xxxxpN9T9mT8wKzmJb|7f17299993b8fb8f3cce3a0xxxxd853bf103c29dae05fa7e99e2b115; act_logged=1528269511'
    trans = transCookie(cookie)
    print (trans.stringToDict())


mycookie = {'wordpress_logged_in_d3bee7395f601ef8b82xxxxde895': 'xxxx|1529479111|gwhpBEo2Wq9DTJsXEFQxxxxppN9T9mT8wKzmJb|7f17299993b8fb8f3cce3a0826c0xxxx03c29dae05fa7e99e2b115', 'act_logged': '1528269511'}
def start_requests(self):
 urls = ['http://www.xxxx.net/xxxxx']
 for url in urls:
 yield scrapy.Request(url=url, callback=self.parse,cookies=mycookie)
码字很辛苦,转载请注明来自人生在世《scrapy爬虫添加cookie》

评论