from fake_useragent import UserAgent
from selenium import webdriver
from scrapy.http.response.html import HtmlResponse
import time
class seleniumMiddleware:
driver = webdriver.Chrome()
def process_request(self, request, spider):
self.driver.get('https://www.douban.com')
time.sleep(5)
zh=input('请输入账号')
mm=input('请输入密码')
time.sleep(3)
self.driver.switch_to.frame(self.driver.find_element_by_xpath('//*[@id="anony-reg-new"]/div/div[1]/iframe'))
time.sleep(1)
self.driver.find_element_by_class_name('account-tab-account').click()
time.sleep(1)
self.driver.find_element_by_name('username').send_keys(zh)
time.sleep(1)
self.driver.find_element_by_name('password').send_keys(mm)
time.sleep(1)
self.driver.find_element_by_class_name('btn').click()
time.sleep(2)
cookies = self.driver.get_cookies()
cookie = [i['name'] + '=' + i['value'] for i in cookies]
cookie_str = '; '.join(i for i in cookie)
sts = {i.split('=')[0]: i.split('=')[1] for i in cookie_str.split('; ')}
print(sts)
response = HtmlResponse(request.url,body=self.driver.page_source,request=request,encoding='utf-8')
return response
获取后可以直接使用sts进行传参
注意这是在scrapy里的middlewares里