序言:
获取采集数据,重要的是节点数据分析,也就是我们常说的审查元素。
代码如下:
# -*- coding: utf-8 -*- """ POM Page Object Model ===》 页面对象模型 一句话解释,就是将页面封装成一个对象; """ from selenium.webdriver.common.by import By from base.base_driver import BaseDriver import random class BasePage: def __init__(self, device_info): self.device_info = BaseDriver(device_info).get_base_driver() self.driver = self.device_info[0] self.desired_caps = self.device_info[1] self.size = dict() """ 将页面封装的元素全部放进来, 我们定位元素的时候,实际上是通过页面元素的定位方式 和定位的值来定位元素 所以在脚本里面,页面元素的代表者就是 定位方式 + 定位的值 """ def swith_to(self, switch=1): if switch == 1: self.swith_to_wechat_native_app() else: self.swith_to_wechat_webview() def swith_to_wechat_webview(self): self.driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0') def swith_to_wechat_native_app(self): self.driver.switch_to.context('NATIVE_APP') def swipUp(self, t=600, n=1): start_x = self.size['width'] * 0.5 start_y = self.size['height'] * 0.75 end_y = self.size['height'] * 0.25 for i in range(n): self.driver.swipe(start_x, start_y, start_x, end_y, t) def swipSmallUp(self, t=300, n=1): t = random.randint(200, 800) start_x = self.size['width'] * 0.5 start_y = self.size['height'] * 0.75 end_y = self.size['height'] * 0.50 for i in range(n): self.driver.swipe(start_x, start_y, start_x, end_y, t) def swipSmallUp2(self, t=200, n=1): t=random.randint(200, 800) start_x = self.size['width'] * 0.5 start_y = self.size['height'] * 0.75 end_y = self.size['height'] * 0.25 for i in range(n): self.driver.swipe(start_x, start_y, start_x, end_y, t) def goodsHotName(self, i) -> str: if i == 1: goods_name = '//*[@id="main"]/div/div[2]/div[2]/div/div[1]/div/div/div[1]/div[1]/div/div/div[2]/div[1]' return self.driver.find_element(By.XPATH, goods_name).text else: goods_name = '//*[@id="main"]/div/div[2]/div[2]/div/div[1]/div/div/div[1]/div[%s]/div/div[2]/div[2]/div[1]' % i return self.driver.find_element(By.XPATH, goods_name).text def goodsName(self, i) -> str: try: goods_name = '//*[@id="mall-list"]/wx-_r/wx-v/wx-v/wx-_c/wx-v/wx-_e[%s]/wx-v/wx-_b/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-v[1]/wx-text/span[2]' % i return self.driver.find_element(By.XPATH, goods_name).text except Exception as e: print("无goods_name_1") try: goods_name = '//*[@id="mall-list"]/wx-_r/wx-v/wx-_d/wx-v/wx-_b/wx-v/wx-_c[%s]/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-v[1]/wx-text/span[2]' % i return self.driver.find_element(By.XPATH, goods_name).text except Exception as e: print("无goods_name_2") return '' def ddmcWechatClick(self): self.driver.find_element(By.XPATH, "//*[@text='多多买菜']").click() def ddmcHotListClick(self,bangdanweizhi): #self.driver.find_element(By.XPATH, '//*[@id="mall-list"]/wx-_e/wx-v/wx-v/wx-_a/wx-v/wx-v/wx-scroll-view/div/div[1]/div/wx-v/wx-_a[6]/wx-v/wx-_a/wx-v/wx-v/wx-v[2]').click() self.driver.find_element(By.XPATH, f'//*[@id="mall-list"]/wx-_e/wx-v/wx-v/wx-_a/wx-v/wx-v/wx-scroll-view/div/div[1]/div/wx-v/wx-_a[{bangdanweizhi}]/wx-v/wx-_a/wx-v/wx-v/wx-v[2]').click() def getwarehouseName(self) -> str: return self.driver.find_element(By.XPATH, '//*[@id="mall-list"]/wx-_d/wx-v/wx-_a/wx-v/wx-v[1]/wx-v[2]').text def dayBuy(self, i) -> str: try: goods_buy_person = '//*[@id="mall-list"]/wx-_r/wx-v/wx-v/wx-_c/wx-v/wx-_e[%s]/wx-v/wx-_b/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-_c[2]/wx-v/wx-v/wx-v[2]/wx-text/span[2]' % i return self.driver.find_element(By.XPATH, goods_buy_person).text except Exception as e: print("无goods_buy_person_info_1") try: goods_buy_person = '//*[@id="mall-list"]/wx-_r/wx-v/wx-_d/wx-v/wx-_b/wx-v/wx-_c[%s]/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-_c[2]/wx-v/wx-v/wx-text/span[2]' % i return self.driver.find_element(By.XPATH, goods_buy_person).text except Exception as e: print("无goods_buy_person_info_1") return '' def imgUrl(self, i) -> str: try: goods_img = '//*[@id="mall-list"]/wx-_r/wx-v/wx-v/wx-_c/wx-v/wx-_e[%s]/wx-v/wx-_b/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[1]/wx-_a/wx-image' % i return self.driver.find_element(By.XPATH, goods_img).get_attribute('src') except Exception as e: print("无goods_img_1") try: goods_img = '//*[@id="mall-list"]/wx-_r/wx-v/wx-_d/wx-v/wx-_b/wx-v/wx-_c[%s]/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[1]/wx-_a/wx-image' % i return self.driver.find_element(By.XPATH, goods_img).get_attribute('src') except Exception as e: print("无goods_img_2") return '' def goodsPrice(self, i) -> str: try: goods_price = '//*[@id="mall-list"]/wx-_r/wx-v/wx-v/wx-_c/wx-v/wx-_e[%s]/wx-v/wx-_b/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-v[2]/wx-v[1]/wx-v/wx-text[2]/span[2]' % i return self.driver.find_element(By.XPATH, goods_price).text except Exception as e: print("无goods_price_1") try: goods_price = '//*[@id="mall-list"]/wx-_r/wx-v/wx-_d/wx-v/wx-_b/wx-v/wx-_c[%s]/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-v[2]/wx-v[1]/wx-v/wx-text[2]/span[2]' % i return self.driver.find_element(By.XPATH, goods_price).text except Exception as e: print("无goods_price_2") return '' def nearBuy(self, i) -> str: goods_buy = '//*[@id="mall-list"]/wx-_r/wx-v/wx-v/wx-_c/wx-v/wx-_e[%s]/wx-v/wx-_b/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-_c[2]/wx-v/wx-v/wx-v[2]/wx-text/span[2]' % i try: return self.driver.find_element(By.XPATH, goods_buy).text except Exception as e: print("无goods_buy_1") goods_buy = '//*[@id="mall-list"]/wx-_r/wx-v/wx-_d/wx-v/wx-_b/wx-v/wx-_c[%s]/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-_c[2]/wx-v/wx-v/wx-v[2]/wx-text/span[2]' % i try: return self.driver.find_element(By.XPATH, goods_buy).text except Exception as e: print("无goods_buy_2") goods_buy = '//*[@id="mall-list"]/wx-_r/wx-v/wx-v/wx-_c/wx-v/wx-_e[%s]/wx-v/wx-_b/wx-_a/wx-v/wx-_a/wx-v/wx-_b/wx-v/wx-v/wx-v[2]/wx-_c/wx-v/wx-v/wx-v[2]/wx-text/span[2]' % i try: return self.driver.find_element(By.XPATH, goods_buy).text except Exception as e: print("无goods_buy_4") return '' 特别声明:做一个好人,用技术改变世界,切勿用学习的技术来破坏网络的环境。让我们一起交流与学习,欢迎私聊。