【问题标题】:No data collected when I extract info from a website using xpath当我使用 xpath 从网站提取信息时没有收集数据
【发布时间】:2021-10-30 11:55:11
【问题描述】:

我需要从网站中提取信息。该网站在以下路径中有信息:

<div class="accordion-block__question">
<div class="accordion-block__text">Server</div></div>
...
<div class="block__col"><b>Country</b></div>

跑步

try: 
            # Country
            c=driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'Country')]").get_attribute('textContent')
            country.append(c)   
except: 
            country.append("Error")

我创建了一个包含所有错误的 df。我对所有领域都感兴趣(但要解决这个问题,只有一个会很棒),包括 Trustscore(数字),但我不知道是否有可能得到它。我在 Chrome 上使用 selenium,网络驱动程序。 网址是https://www.scamadviser.com/check-website。

代码

这是完整的代码:

def scam(df):
    chrome_options = webdriver.ChromeOptions()

    trust=[]
    country = [] 
    isp_country = [] 
        
    query=df['URL'].unique().tolist() 
    driver=webdriver.Chrome('mypath',chrome_options=chrome_options))
    
    for x in query:
        
        wait = WebDriverWait(driver, 10)
        response=driver.get('https://www.scamadviser.com/check-website/'+x)
        
        try: 
            wait = WebDriverWait(driver, 30)
            # missing trustscore

            # Country
            c=driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'Country')]")).get_attribute('innerText')
            country.append(c)  

            # ISP country
        ic=driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'ISP')]").get_attribute('innerText')
            isp_country.append(ic)
        
        except: 
            # missing trustscore
            country.append("Error")
            isp_country.append("Error")
            

    # Create dataframe
    dict = {'URL': query, 'Trustscore':trust, 'Country': country, 'ISP': isp_country} 
    df=pd.DataFrame(dict)

    driver.quit()
    
    return df

您可以尝试例如 df['URL'] 等于

stackoverflow.com
gitHub.com

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping webdriver


    【解决方案1】:

    您正在寻找innerText 而不是textContent。

    代码:

    try: 
      # Country
      c = driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'Country')]").get_attribute('innerText')
      print(c)
      country.append(c)   
    except: 
       country.append("Error")
    

    更新 1:

    如果已经使用的定位器是正确的。

    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'Country')]"))
    

    或者可以尝试使用此 xpath 的两个选项:-

    //div[contains(@class,'block__col')]/b[text()='Country']
    

    更新 2:

    尝试: 等待= WebDriverWait(驱动程序,30) # 缺少信任分数

    # Country
    time.sleep(2)
    ele = driver.find_element_by_xpath("//div[contains(@class,'block__col')]/b[text()='Country']")
    driver.execute_script("arguments[0].scrollIntoView(true);", ele)
    country.append(ele.get_attribute('innerText'))
    
    time.sleep(2)
    # ISP country
    ic = driver.find_element_by_xpath("//div[contains(@class,'block__col')]/b[text()='ISP']")
    driver.execute_script("arguments[0].scrollIntoView(true);", ele)
    isp_country.append(ic.get_attribute('innerText'))
    

    更新 3:

    获取Company data、Country name。

    使用这个 xpath :

    //div[text()='Company data']/../following-sibling::div/descendant::b[text()='Country']/../following-sibling::div
    

    另外,在使用此 xpath 之前,请确保几件事。

    1. 以全屏模式启动浏览器。
    2. 使用 js 滚动,然后使用 sroll 进入视图或动作链。

    代码:-

    driver.maximize_window()
    time.sleep(2)
    driver.execute_script("window.scrollTo(0, 1000)")
    time.sleep(2)
    driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Company data']"))))
    # now use the mentioned xpath.
    
    company_data_country_name` = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Company data']/../following-sibling::div/descendant::b[text()='Country']/../following-sibling::div")))
    print(company_data_country_name.text)
    

    【讨论】:

    • 你能分享页面网址吗?
    • 是的,我们需要唯一地定位它们,让我再看一下,所以基本上两个国家/地区来自Server (ISP country) 和Company data 对吗?
    • 您可以尝试更新上面的 3 部分并告诉我是否可行吗?
    • 非常感谢您提供的所有帮助,@cruisepandey。是的,它有效。我将自己尝试使 df 更新正常工作。
    • @LdM :当然,欢迎您在需要时创建新票证。
    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多