Python 如何获取URL页面的结果值?

Python 如何获取URL页面的结果值?,python,python-3.x,selenium,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Chromedriver,我正在尝试自动获取URL缩短的结果。 这是我正在使用的页面: 这是我编写的代码(URL列表包含链接): 在此之后,我得到了缩短的url。我需要一些帮助才能获得它。使用WebDriverWait等待简短的url结果并获取值 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from sel

我正在尝试自动获取URL缩短的结果。 这是我正在使用的页面:

这是我编写的代码(URL列表包含链接):


在此之后,我得到了
缩短的url
。我需要一些帮助才能获得它。

使用
WebDriverWait
等待简短的url结果并获取值

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

with driver:
    driver.get("http://paylinx.pw/linx/")
    for url in URLS:
        driver.find_element_by_id("url").send_keys(url, Keys.ENTER)

        short_url = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".add-link-result .input-lg"))).get_attribute("value")
        print(short_url, url)

您需要使用page.source,因为它可以根据需要翻译页面代码(对于chromedriver,如lifehuck),或者您可以使用get_属性('innerHTML')-您可以访问页面中的所有内容


要自动提取URL缩短器的结果值,您需要为位于()的元素的可见性引入WebDriverWait,并且您可以使用以下方法:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    URLS = ['https://selenium.dev/downloads/','https://selenium.dev/documentation/en/']
    for i in URLS:
        driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
        driver.get("http://paylinx.pw/linx/")
        element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#url")))
        element.clear()
        element.send_keys(i)
        driver.find_element_by_css_selector("button.btn-captcha#invisibleCaptchaShort").click()
        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.input-group>input.form-control.input-lg"))).get_attribute("value"))
        driver.quit()
    
  • 控制台输出:

    http://paylinx.pw/linx/Uksheqw8
    http://paylinx.pw/linx/s0DA44C
    

查看驱动程序。page\u source,它可能就在那里的某个地方
http://paylinx.pw/linx/Uksheqw8
http://paylinx.pw/linx/s0DA44C