1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| 复制def login_jd(self, name, pwd): """ 登录京东平台 :param pwd: 账号 :param name: 密码 :return: """
def Drag_the(block, juli, aLocal, H, W): """ 用于移动滑块 """ x = block.location.get('x') y = block.location.get('y') x1 = aLocal.get('x') y1 = aLocal.get('y') pyautogui.moveTo(x + x1 + W, y + y1 + H, duration=0.1) pyautogui.mouseDown() # 分为多段滑动 count = 0 i = 1 o = 0 while count < juli * 0.8: q = random.randint(10, 20) if q > o: # print(q) count += q pyautogui.moveTo(x + x1 + W + count, y + y1 + H, duration=random.randint(1, 9) / (i * 100)) o = q i += 1 elif q == o: # print(q) count += q pyautogui.moveTo(x + x1 + W + count, y + y1 + H, duration=random.randint(1, 9) / (i * 100)) o = 0 i += 1 o = 10 while count >= juli * 0.8 and count < juli: q = random.randint(1, 8) if q <= o: # print(str(q)+"..") count += q pyautogui.moveTo(x + x1 + W + count, y + y1 + H, duration=random.randint(1, 9) / (i * 100)) pyautogui.moveTo(x + x1 + W + juli - 2, y + y1 + H, duration=0.2) time.sleep(0.8) pyautogui.moveTo(x + x1 + W + juli - 1, y + y1 + H, duration=0.2) pyautogui.mouseUp()
self.driver.get('https://shop.jd.com/') time.sleep(2) self.driver.maximize_window() try: # 如果当前已有账号登录,且账号是对的 if self.find("//span[@class='shop-pageframe-header__pin']").text == name: return True else: # 如果当前已有账号登录,且非指定账号,则退出 self.logout_jd() except BaseException: pass # 获取屏幕大小 winW, winH = pyautogui.size() self.sure_click("//div[contains(text(), '账户登录')]") a = self.driver.find_element_by_id('loginFrame') # 获得iframe的网页位置 iframeLocal = a.location h = self.find('//body') # 获得网页body的大小 bodySize = h.size # 获得网页与屏幕顶端的高度差 WHH = winH - bodySize.get('height') self.driver.switch_to.frame(a) self.driver.find_element_by_id('loginname').clear() self.driver.find_element_by_id('loginname').send_keys(name) self.driver.find_element_by_id('nloginpwd').clear() self.driver.find_element_by_id('nloginpwd').send_keys(pwd) try: time.sleep(4) self.driver.find_element_by_id('paipaiLoginSubmit').click() except: pass time.sleep(1) while True: try: bl = self.driver.find_element_by_class_name('JDJRV-smallimg > img') except NoSuchElementException: bl = False if bl != False: # 用于找到登录图片的大图 bigimg = self.driver.find_element_by_class_name('JDJRV-bigimg > img').get_attribute("src") # 获得网页的显示的图片宽度 htmlsize = self.driver.find_element_by_class_name('JDJRV-bigimg').size['width'] # 用来找到登录图片的小滑块 smallimg = self.driver.find_element_by_class_name('JDJRV-smallimg > img').get_attribute("src") # 背景大图命名 backimg = "backimg.png" # 滑块命名 slideimg = "slideimg.png" # 下载背景大图保存到本地 request.urlretrieve(bigimg, backimg) # 下载滑块保存到本地 request.urlretrieve(smallimg, slideimg) # 获取图片并灰度化 block = cv2.imread(slideimg, 0) template = cv2.imread(backimg, 0) # 获取下载下来大图的宽 bigsize = list(template.shape)[1] # 获取下载下来滑块的宽 slidesize = list(block.shape)[1]
# 获得缩放比例 bili = htmlsize / bigsize # print(bili) # 二值化后的图片名称 blockName = "block.jpg" templateName = "template.jpg" # 将二值化后的图片进行保存 cv2.imwrite(blockName, block) cv2.imwrite(templateName, template) block = cv2.imread(blockName) block = cv2.cvtColor(block, cv2.COLOR_RGB2GRAY) block = abs(255 - block) cv2.imwrite(blockName, block) block = cv2.imread(blockName) template = cv2.imread(templateName) # 获取偏移量 result = cv2.matchTemplate(block, template, cv2.TM_CCOEFF_NORMED) # 查找block在template中的位置,返回result是一个矩阵,是每个点的匹配结果 # print(result) x, y = np.unravel_index(result.argmax(), result.shape) # print("x方向的偏移", int(y*bili), 'x:', x, 'y:', y) # 获取滑块 element = self.driver.find_element_by_class_name('JDJRV-smallimg > img') Drag_the(element, y * bili, iframeLocal, WHH, slidesize * bili / 2) time.sleep(3) else: break
# 看是否需要短信验证 while True: try: self.find("//a[text()='短信验证']").click() tips('需要短信验证,请手动完成后点击确定') break except: break
while True: if self.driver.title == '京东商城:商家后台': self.obvious_wait("//div[@aria-label='公告提醒']/button") # 存在提示信息,点击关闭 self.obvious_wait("//div[@aria-label='京麦提示']/button") # 存在提示信息,点击关闭 break
|