【问题标题】:How to extract the title from the given anchor tag如何从给定的锚标签中提取标题
【发布时间】:2019-06-22 01:13:04
【问题描述】:

如何获取 xpath 以从此 html 行中提取标题。

没有得到任何有用的东西,因为 cssClass 会随时间变化,因此代码可能会中断。我认为由于此标记中的 href 和 text 都是我要提取的名称,因此可能使用相等条件。

<a class="FPmhX notranslate nJAzx" title="ceorackz_adpp" href="/ceorackz_adpp/">ceorackz_adpp</a>

我希望 python 代码兼容使用 selenium API 调用或普通正则表达式来获取此锚标记的标题或文本。

【问题讨论】:

  • 请添加您的一些代码

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

右键单击检查部分中的HTML 元素。 然后去Copy &gt; Copy XPath。 然后使用此代码

title = driver.find_element_by_xpath("copied_xpath").get_attribute("title")
href = driver.find_element_by_xpath("copied_xpath").get_attribute("href")
text = driver.find_element_by_xpath("copied_xpath").text

【讨论】:

    【解决方案2】:

    使用以下列表中的任何 xpath:

    //a[@title='ceorackz_adpp']
    
    //a[text()='ceorackz_adpp']
    
    //a[@title='ceorackz_adpp' and text()='ceorackz_adpp']
    

    【讨论】:

      【解决方案3】:

      要从元素中提取标题,即 ceorackz_adpp,您必须为 visibility_of_element_located() 诱导 WebDriverWait,您可以使用以下任一解决方案:

      • 使用CSS_SELECTOR

        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.notranslate[href='/ceorackz_adpp/']"))).get_attribute("title"))
        
      • 使用LINK_TEXT

        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "ceorackz_adpp"))).get_attribute("title"))
        
      • 使用XPATH

        print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@class, 'notranslate') and @href='/ceorackz_adpp/']"))).get_attribute("title"))
        
      • 注意:您必须添加以下导入:

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

      【讨论】:

        【解决方案4】:

        我不太确定,但我猜是这样的,可能类似于:

        title="(.+?)">\s*(.+?)\s*<
        

        可能是一个起点。

        Demo

        测试

        import re
        
        regex = r"title=\"(.+?)\">\s*(.+?)\s*<"
        
        test_str = "<a class=\"FPmhX notranslate nJAzx\" title=\"ceorackz_adpp\" href=\"/ceorackz_adpp/\">ceorackz_adpp</a>"
        
        matches = re.finditer(regex, test_str, re.DOTALL)
        
        for matchNum, match in enumerate(matches, start=1):
            
            print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
            
            for groupNum in range(0, len(match.groups())):
                groupNum = groupNum + 1
                
                print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多