美文网首页
selenium 临时使用记录

selenium 临时使用记录

作者: 夙小叶 | 来源:发表于2021-08-12 11:33 被阅读0次
    截屏2021-08-12 11.32.22.png
    def webdrive_get2(url: str):
        random_agent = USER_AGENTS[randint(0, len(USER_AGENTS) - 1)]
        op = Options()
        op.add_argument("--headless")
        op.add_argument("--disable-gpu")
        op.add_argument(f'user-agent="{random_agent}"')
        op.add_argument("--proxy-server=http://127.0.0.1:7890")
        dr = "/Users/rabbit/opt/selenium/webDriver/chromedriver"
        chromeA = webdriver.Chrome(options=op, executable_path=dr)
        chromeA.get(url)
    
        try:
            u = WebDriverWait(chromeA, 3, 2).until(EC.presence_of_all_elements_located((By.TAG_NAME, "td")))
            # print(u[1].text)
            for i in u:
                print(i.text)
        finally:
            # XPATH 测试
            a = chromeA.find_element(By.XPATH, "/html/body/table/tbody/tr[2]")
            print(a.text)
            chromeA.quit()
    
    截屏2021-08-12 10.19.19.png
    def webdriver_get(url: str):
        random_agent = USER_AGENTS[randint(0, len(USER_AGENTS) - 1)]
        op = Options()
        op.add_argument("--headless")
        op.add_argument("--disable-gpu")
        op.add_argument(f'user-agent="{random_agent}"')
        op.add_argument("--proxy-server=http://127.0.0.1:7890")
        dr = "/Users/ruiliang/opt/selenium/webDriver/chromedriver"
        chromeA = webdriver.Chrome(options=op, executable_path=dr)
        chromeA.get(url)
    
        print(f"Down: \t{chromeA.title} \n")
    
        u = WebDriverWait(chromeA, 5).until(lambda x: x.find_elements(By.XPATH, "/html/body/div/div[2]/div[4]/div/ul/li"))
        # print(f"Master => {chromeA.current_window_handle}")
        for i in track(range(len(u))):
            u = WebDriverWait(chromeA, 5).until(
                lambda x: x.find_elements(By.XPATH, "/html/body/div/div[2]/div[4]/div/ul/li"))
            ui = u[i].find_element_by_tag_name("a").get_attribute("href")
    
            chromeA.get(ui)
            # print(f"New Page => {chromeA.current_window_handle}")
    
            p = WebDriverWait(chromeA, 3).until(lambda x: x.find_element_by_class_name("fitVertical"))
            img = p.find_element_by_tag_name("img").get_attribute("src")
    
            filename = str(img).split("/")[-1]
            suffix = str(img).split(".")[-1]
            var1 = "%05d" % i
            full_name = str(var1) + "." + suffix
            print("Down: \t" + filename + "\t" + full_name)
    
            stream = send_request(img)
            write_file(full_name, stream)
    
            chromeA.back()
        chromeA.quit()
    
    截屏2021-08-12 12.34.30.png

    相关文章

      网友评论

          本文标题:selenium 临时使用记录

          本文链接:https://www.haomeiwen.com/subject/yszkbltx.html