scrapy模拟登录值携带cookie
登录人人网的一个小例子:
1 # -*- coding: utf-8 -*- 2 import scrapy 3 import re 4 5 class RenrenSpider(scrapy.Spider): 6 name = 'renren' 7 allowed_domains = ['renren.com'] 8 start_urls = ['http://www.renren.com/469175544/profile'] 9 10 #重写 start_requests函数 11 def start_requests(self): 12 cookies = "anonymid=k9qu49f8-u2i9f6; depovince=GW; _r01_=1; JSESSIONID=abcyifRVJlHZa8oZrZzhx; ick_login=0e0c11f7-bf9d-459c-a90e-58cd4c4080d1; taihe_bi_sdk_uid=1484c0da41eccfff893c567343c25dfc; taihe_bi_sdk_session=5593c355d7a1c951d83c0bea940cee01; _de=DFB3AA5FF07DCEC55398183FA2F16FA6; ick=82f1abf5-42ab-4a45-bf6e-5b62ffa52a32; __utma=151146938.214793330.1588497096.1588497096.1588497096.1; __utmc=151146938; __utmz=151146938.1588497096.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmb=151146938.4.10.1588497096; jebecookies=01c2365d-44a6-4092-9350-7d4aee6560f7|||||; p=d3581425a61f49a0d4299705fff295624; first_login_flag=1; ln_uact=18398915957; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; t=d939d694f6359a2d8bf5ed0f056569e24; societyguester=d939d694f6359a2d8bf5ed0f056569e24; id=469175544; xnsid=2630ca7c; ver=7.0; loginfrom=null; jebe_key=25bfd9a1-c482-4cb9-a0a2-dee18f5d7f00%7Ca4a12ab29ed67d86d0e62a032248ff26%7C1588497169902%7C1%7C1588497169522; jebe_key=25bfd9a1-c482-4cb9-a0a2-dee18f5d7f00%7Ca4a12ab29ed67d86d0e62a032248ff26%7C1588497169902%7C1%7C1588497169526; XNESSESSIONID=d39262cd4fdb; wp_fold=0; wp=0" 13 cookies = {i.split('=')[0]: i.split('=')[1] for i in cookies.split(';')} 14 yield scrapy.Request( 15 self.start_urls[0], 16 callback = self.parse, 17 cookies = cookies 18 ) 19 20 def parse(self, response): 21 print(re.findall('落雨', response.body.decode())) 22 yield scrapy.Request( 23 'http://www.renren.com/469175544/profile?v=info_timeline', 24 callback = self.parse_detail 25 ) 26 27 def parse_detail(self,response): 28 print(re.findall('落雨', response.body.decode()))
--------------------成功,肯定是需要一点一滴积累的--------------------