def request(self, taobao_request): """ 执行请求 :param taobao_request: 请求 :return: 响应 """ try: if taobao_request.need_proxy: proxy = self.get_proxy() if proxy: proxies = { 'http': 'http://' + proxy, 'https': 'https://' + proxy } logger.info(f'Get proxy {proxies}') return SESSION.get(url=taobao_request.url, headers=self.headers, timeout=taobao_request.timeout, proxies=proxies) return SESSION.get(url=taobao_request.url, headers=self.headers, timeout=taobao_request.timeout) except (ConnectionError, ReadTimeout) as e: print(e.args) return False
def _html(self): """ 获取登录页面代码 :return: self._html """ response = SESSION.get(url=self.login_url, headers=self.headers, verify=False) return response.text
def verify_login(self, response): """ 模拟验证 :param response: :return: """ iframe_url = response.json()['content']['data']['iframeRedirectUrl'] iframe_response = SESSION.get(url=iframe_url, headers=self.headers, verify=False) remote_url = re.search(r'<iframe src="(.*?)"', iframe_response.text).group(1) remote_response = SESSION.get(url=remote_url, headers=self.headers, verify=False) normal_url = remote_response.url normal_response = SESSION.get(url=normal_url, headers=self.headers, verify=False) verify_url = re.search(r'window.location.href = "(.*?)"', normal_response.text).group(1) + "1" verify_response = SESSION.get(url=verify_url, headers=self.headers, verify=False) safe_login_url = re.search(r'window.location.href = "(.*?)"', verify_response.text).group(1) safe_login_response = SESSION.get(url=safe_login_url, headers=self.headers, verify=False) self.redirect_url = re.search(r'"redirectUrl":"(.*?)"', safe_login_response.text).group(1)
def print_title(self): """ 输出重定向页面后的标题,以验证登录 :return: """ try: response = SESSION.get(url=self.redirect_url, headers=self.headers, verify=False) response.raise_for_status() self.content = response.text # # 有必要时保存第一页代码,便于调试 # with open('success.html', 'w', encoding='utf-8')as file: # file.write(content) match = re.search(r'<title>(.*?)</title>', self.content, re.S) title = match.group(1) if title != f'{PRODUCT}_淘宝搜索': raise TitleError(f'标题错误,标题:{title}') except TitleError as e: raise e else: logger.info(f'网页标题为:{title}')
def logged(self): """ 模拟登录 :return: bool """ if self.load_cookies(): return False post_data = { 'loginId': self.loginId, 'password2': self.password2, 'keepLogin': '******', 'ua': self.ua, # 'umidGetStatusVal': '255', # 'screenPixel': '1536x864', # 'navlanguage': 'zh-CN', 'navUserAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36', 'navPlatform': 'Win32', 'appName': 'taobao', 'appEntrance': 'taobao_pc', '_csrf_token': self.get_value('_csrf_token'), 'umidToken': self.get_value('umidToken'), 'hsiz': self.get_value('hsiz'), 'bizParams': None, # 'style': 'default', 'appkey': '00000000', 'from': 'tb', 'isMobile': 'false', # 'lang': 'zh-CN', 'returnUrl': self.redirect_url, 'fromSite': '0' } headers = { 'Host': 'login.taobao.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36', 'Accept': 'application/json, text/plain, */*', # 'Accept-Language': 'zh-CN,en-US;q=0.7,en;q=0.3', # 'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'application/x-www-form-urlencoded', 'Origin': 'https://login.taobao.com', 'Connection': 'keep-alive', 'Referer': self.login_url, } try: response = SESSION.post(url=self.commit_url, headers=headers, data=post_data, verify=False) response.raise_for_status() except Exception as e: logger.error(f'登录失败,原因:') raise e self.queue_cookies() if 'iframeRedirectUrl' in response.json()['content']['data']: logger.warning('需要安全验证') self.verify_login(response) else: self.redirect_url = response.json( )['content']['data']['redirectUrl'] return True