8_3 scrapy模拟登录人人网
一、创建项目
二、更改设置(setting等)
三、编码
# -*- coding: utf-8 -*- import scrapy class RenrenSpider(scrapy.Spider): name = ‘renren‘ allowed_domains = [‘renren.com‘] start_urls = [‘http://renren.com/‘] def start_requests(self): #url = "http://www.renren.com/SysHome.do" url = "http://www.renren.com/PLogin.do" data = {"email": "邮箱", "password": "密码"} request = scrapy.FormRequest(url, formdata=data, callback=self.parse_page) yield request def parse_page(self, response): # with open(‘renren.html‘, ‘w‘, encoding=‘utf-8‘) as fp: # fp.write(response.text) # 登录后要访问的页面大鹏懂成 url = "http://www.renren.com/880151247/profile" request = scrapy.Request(url, callback=self.parse_profile) yield request def parse_profile(self, response): with open(‘dapeng.html‘, ‘w‘, encoding=‘utf-8‘) as fp: fp.write(response.text)
四、总结:
1、想要发送post请求,那么推荐使用scrapy.FormRequest()方法
2、如果想在爬虫一开始的时候的送post请求,那么要重写基类Spider中的方法 start_request()
方法。在这个方法中发送post请求,没有重写这个方法基类Spider中的方法 start_request()默认是
发送get请求。
相关推荐
ZHANGRENXIANG00 2020-06-28
Catastrophe 2020-06-26
andrewwf 2020-05-30
ZHANGRENXIANG00 2020-05-11
Catastrophe 2020-05-07
andrewwf 2020-11-11
Arvinzx 2020-10-28
CycloneKid 2020-10-27
paleyellow 2020-10-25
baifanwudi 2020-10-25
heyboz 2020-10-21
wumxiaozhu 2020-10-16
ZHANGRENXIANG00 2020-07-27
zhangll00 2020-07-05
Catastrophe 2020-06-26
fangjack 2020-06-25
andrewwf 2020-06-16