作为爬虫的我们都知道,cookie是一些网站为了识别用户身份,进行会话跟踪而存储在用户本地终端的数据。比如有些网站需要登录才能访问某个页面,而不允许在登录前抓取某个页面的内容,这时候我们就需要通过保存登录的方式来获取cookie,然后进行爬取页面,从而达到我们的目的。爬虫程序中有很多语言可以用来存储cookies来模拟登录信息。比如Selenium登录生成cookies。下面通过一个简单的例子来说明如何获取这个cookie。importos导入时间importzipfilefromseleniumimportwebdriverfromselenium.common.exceptionsimportTimeoutExceptionfromselenium.webdriver.common.byimportByfromselenium.webdriver.supportimportexpected_conditionsasECfromselenium.webdriver.support.uiimportWebDriverWaitclassGenCookies(object):#随机useragentUSER_AGENT=open('useragents.txt').readlines()#代理服务器(产品官网www.16yun.cn)PROXY_HOST='t.16yun.cn'##X1proxyorhostportproxy_user='用户名'#用户名proxy_pass='密码'#密码@classmethoddefdefget_chromedriver(cls,use_proxy=false,user_agent=none):2,“名称”:“Chrome代理”,“权限”:[“代理”,“Tabs”,“UnlimitedStorage”,“存储”,“”,“WebRequest”,“WebRequestBlocking”],“背景”:{“scripts”:[“background.js”]},“minimum_chrome_version”:“22.0.0.0”}“”“background_js=”“”varconfig={mode:“fixed_servers”,规则:{singleproxy:{schone:“http”,host:“%s”,port:parseint(parseint(%s)},旁路列表:[localhost“]}};chrome.proxy.settings.set({value:config:config,scope:“juromant”},function(){});functioncallbackfn(详细信息){return{return{authcredentials:{用户名:,密码:“%s”}};}chrome.webRequest.onAuthRequired.addListener(callbackFn,{urls:[""]},['阻塞']);"""%(cls.PROXY_HOST,cls.PROXY_PORT,cls.PROXY_USER,cls.PROXY_PASS)path=os.path.dirname(os.path.abspath(__file__))chrome_options=webdriver.ChromeOptions()的#关闭一webdrive标志##chrome_options.add_experiment_option('excludesWitches',['enable-automation'])如果use_proxy:pluginfile='proxy_auth_plugin.zip'withzipfile.zipfile.zipfile.zipfile(pluginfile.zipfile(pluginfile)",manifest_json)zp.writestr("background.js",background_js)chrome_options.add_extension(pluginfile)ifuser_agent:chrome_options.add_argument('--user-agent=%s'%user_agent)驱动程序=webdriver.Chrome(os.path.join(路径,'chromedriver'),chrome_options=chrome_options)#webdrivergetwebdrect=''''#object.defineProperty(#get:()=>undefined#})#''''#driver.execute_cdp_cmd(“page.addscripttoevaluateonnewdocument”,{source})返回drefdrefdef__init__(self,username,username,passward)self.url='https://passport.example.cn/s...://m.example.cn/'self.browser=self.get_chromedriver(use_proxy=True,user_agent=self.USER_AGENT)self.wait=webdriverwait(self.browser,20)self.username=usernameself.password=passworddefopen(self):“”“”打开打开打开网页输入用户名密码并并:return:none:none“self.browser.browser.delete_allete_all_all_all_all_cookies()self.browser.get(self.url)用户名??=self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))password=self.wait.until(EC.presence_of_element_located((By.ID,'loginPassword')))submit=self.wait.until(EC.element_to_be_clickable((By.ID,'loginAction')))username.send_keys(self.username)password.send_keys(self.password)1time.sleep()crives.Click()defpassword_error(self):“”“”判断判断密码错误:返回:“'),','用户名或密码')))除timeoutexception外:返回falsefalsedefget_cookies(self):“”“”获取cookies:return:“”“returnself.browser.get_cookies()defmain()defmain(self):“”"入口:return:""self.open()ifself.password_error():return{'STATUS':2,'Content':'用户名或密码错误'}cookies=Self.get_cookies()retUrn{'Status':1,'Content':Cookies}if__Name__=='__main___':Result=GenCookies(username='180000000',password='16yun',).,这里先分享其中的一种,掌握多种方法也能提高我们的工作效率