Best Python code snippet using SeleniumBase
zhihu.py
Source:zhihu.py
...48 :return: bool49 è¥å¨ PyCharm ä¸ä½¿ç¨ä¸æéªè¯åºç°æ æ³ç¹å»çé®é¢ï¼50 éè¦å¨ Settings / Tools / Python Scientific / Show Plots in Toolwindowï¼åæ¶å¾é51 """52 if load_cookies and self.load_cookies():53 print('读å Cookies æ件')54 if self.check_login():55 print('ç»å½æå')56 return True57 print('Cookies å·²è¿æ')58 self._check_user_pass()59 self.login_data.update({60 'username': self.username,61 'password': self.password,62 'lang': captcha_lang63 })64 timestamp = int(time.time() * 1000)65 self.login_data.update({66 'captcha': self._get_captcha(self.login_data['lang']),67 'timestamp': timestamp,68 'signature': self._get_signature(timestamp)69 })70 headers = self.session.headers.copy()71 headers.update({72 'content-type': 'application/x-www-form-urlencoded',73 'x-zse-83': '3_1.1',74 'x-xsrftoken': self._get_xsrf()75 })76 data = self._encrypt(self.login_data)77 login_api = 'https://www.zhihu.com/api/v3/oauth/sign_in'78 resp = self.session.post(login_api, data=data, headers=headers)79 if 'error' in resp.text:80 print(json.loads(resp.text)['error'])81 if self.check_login():82 print('ç»å½æå')83 return True84 print('ç»å½å¤±è´¥')85 return False86 def load_cookies(self):87 """88 读å Cookies æ件å è½½å° Session89 :return: bool90 """91 try:92 self.session.cookies.load(ignore_discard=True)93 return True94 except FileNotFoundError:95 return False96 def check_login(self):97 """98 æ£æ¥ç»å½ç¶æï¼è®¿é®ç»å½é¡µé¢åºç°è·³è½¬åæ¯å·²ç»å½ï¼99 å¦ç»å½æåä¿åå½å Cookies100 :return: bool...
abstract.py
Source:abstract.py
...12 self.session.headers.update({'user-agent': user_agent})13 def save_cookies(self):14 with open(self.COOKIES_FILE, 'wb') as f:15 pickle.dump(self.session.cookies, f)16 def load_cookies(self):17 if not os.path.isfile(self.COOKIES_FILE):18 return False19 with open(self.COOKIES_FILE, 'rb') as f:20 self.session.cookies.update(pickle.load(f))21 return True22 # tries to load cookies and if the user is not still logged in it uses23 # the children's method of login and saves the cookies24 # load_cookies should be false if you know the cookies are already invalid25 def login(self, load_cookies=True):26 if load_cookies:27 self.load_cookies()28 if self._verify_login():29 return30 self._login()31 self.save_cookies()32# used to verify the scraper is logged in33def _logged_in(func):34 @wraps(func)35 def wrapper(inst, *args, **kwargs):36 if not inst._verify_login():37 inst.login(load_cookies=False)38 return func(inst, *args, **kwargs)...
session.py
Source:session.py
...7 @staticmethod8 def instance():9 if CianReqSession._instance is None:10 CianReqSession._instance = requests.session()11 CianReqSession.load_cookies()12 return CianReqSession._instance13 @staticmethod14 def load_cookies():15 if CianReqSession._instance is None:16 return17 if os.path.isfile(COOKIES_FILENAME):18 with open(COOKIES_FILENAME, 'rb') as file:19 CianReqSession._instance.cookies.update(pickle.load(file))20 @staticmethod21 def save_cookies():22 if CianReqSession._instance is None:23 return24 with open(COOKIES_FILENAME, 'wb') as file:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!