【问题标题】:How to get_attribute('innerHTML') from a list of URLs - Selenium?如何从 URL 列表中获取属性('innerHTML') - Selenium?
【发布时间】:2019-07-17 00:12:02
【问题描述】:

我在 Python 中使用 Selenium 进行网页抓取。我正在使用 xpath 来提取网站的部分内容。

我想知道如何使用循环提取 URL 列表并将它们保存到字典中。

mylist_URLs = ['https://www.sec.gov/cgi-bin/own-disp? action=getowner&CIK=0001560258',
'https://www.sec.gov/cgi-bin/own-disp?action=getissuer&CIK=0000034088',
'https://www.sec.gov/cgi-bin/own-disp?action=getissuer&CIK=0001048911']

我下面的编码仅适用于 1 个网址...

driver = webdriver.Chrome(r'xxx\chromedriver.exe')
driver.get('https://www.sec.gov/cgi-bin/own-disp?action=getowner&CIK=0000104169')

driver.find_elements_by_xpath('/html/body/div/table[1]/tbody/tr[2]/td/table/tbody/tr[1]/td')[0].get_attribute('innerHTML')

感谢您的帮助。

【问题讨论】:

    标签: python loops selenium


    【解决方案1】:

    您可以对每个循环使用简单的 WebDriverWait 来确保在获取 innerHTML 之前加载表格。

    添加以下导入:

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

    脚本:

    mylist_URLs = ['https://www.sec.gov/cgi-bin/own-disp? action=getowner&CIK=0001560258',
    'https://www.sec.gov/cgi-bin/own-disp?action=getissuer&CIK=0000034088',
    'https://www.sec.gov/cgi-bin/own-disp?action=getissuer&CIK=0001048911']
    # open the browser
    driver = webdriver.Chrome(r'xxx\chromedriver.exe')
    # iterate through all the urls
    for url in mylist_URLs:
        print(url)
        driver.get(url)
        # wait for the table to present
        element = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH, "(//table[1]/tbody/tr[2]/td/table/tbody/tr[1]/td)[1]"))
        # now get the element innerHTML
        print(element.get_attribute('innerHTML')))
    

    【讨论】:

    • 我收到了“语法错误:解析 #element.get_attribute('innerHTML') 时出现意外 EOF”。我还发现“语法错误:解析元素时出现意外 EOF = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,”(//table[1]/tbody/tr[2]/td/table/ tbody/tr[1]/td)[1]"))"
    • 用行尾缺少的) 更新了答案。
    • 嗯。仍然收到“SyntaxError: invalid syntax : print(element.get_attribute('innerHTML')))”
    • 解决了! element = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH, "(//table[1]/tbody/tr[2]/td/table/tbody/ tr[1]/td)[1]"))
    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多