<small id='56QUd'></small><noframes id='56QUd'>

  1. <tfoot id='56QUd'></tfoot>

      <bdo id='56QUd'></bdo><ul id='56QUd'></ul>

  2. <legend id='56QUd'><style id='56QUd'><dir id='56QUd'><q id='56QUd'></q></dir></style></legend>
    <i id='56QUd'><tr id='56QUd'><dt id='56QUd'><q id='56QUd'><span id='56QUd'><b id='56QUd'><form id='56QUd'><ins id='56QUd'></ins><ul id='56QUd'></ul><sub id='56QUd'></sub></form><legend id='56QUd'></legend><bdo id='56QUd'><pre id='56QUd'><center id='56QUd'></center></pre></bdo></b><th id='56QUd'></th></span></q></dt></tr></i><div id='56QUd'><tfoot id='56QUd'></tfoot><dl id='56QUd'><fieldset id='56QUd'></fieldset></dl></div>

      抓取动态数据硒-无法定位元素

      时间:2024-04-20
      <i id='VKI9X'><tr id='VKI9X'><dt id='VKI9X'><q id='VKI9X'><span id='VKI9X'><b id='VKI9X'><form id='VKI9X'><ins id='VKI9X'></ins><ul id='VKI9X'></ul><sub id='VKI9X'></sub></form><legend id='VKI9X'></legend><bdo id='VKI9X'><pre id='VKI9X'><center id='VKI9X'></center></pre></bdo></b><th id='VKI9X'></th></span></q></dt></tr></i><div id='VKI9X'><tfoot id='VKI9X'></tfoot><dl id='VKI9X'><fieldset id='VKI9X'></fieldset></dl></div>

    1. <legend id='VKI9X'><style id='VKI9X'><dir id='VKI9X'><q id='VKI9X'></q></dir></style></legend>
        • <bdo id='VKI9X'></bdo><ul id='VKI9X'></ul>

            <small id='VKI9X'></small><noframes id='VKI9X'>

          • <tfoot id='VKI9X'></tfoot>
                <tbody id='VKI9X'></tbody>
              1. 本文介绍了抓取动态数据硒-无法定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是刮毛的新手,我有一个问题。我在搜集Worldeter的Covid数据。因为它是动态的-我正在用硒做这件事。

                代码如下:

                from selenium import webdriver
                import time
                
                URL = "https://www.worldometers.info/coronavirus/"
                
                # Start the Driver
                driver = webdriver.Chrome(executable_path = r"C:Webdriverchromedriver.exe")
                # Hit the url and wait for 10 seconds.
                driver.get(URL)
                time.sleep(10)
                #find class element
                data= driver.find_elements_by_class_name("odd" and "even")
                #for loop
                for d in data:
                    country=d.find_element_by_xpath(".//*[@id='main_table_countries_today']").text
                    print(country)
                

                当前输出:

                NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='main_table_countries_today']"}
                  (Session info: chrome=96.0.4664.45)
                

                推荐答案

                要在worldometers covid data中刮取表,您需要为visibility_of_element_located()归纳WebDriverWait,使用DataFramePandas可以使用以下Locator Strategy:

                挡路代码:

                from selenium import webdriver
                from selenium.webdriver.chrome.options import Options
                from selenium.webdriver.chrome.service import Service
                from selenium.webdriver.support.ui import WebDriverWait
                from selenium.webdriver.common.by import By
                from selenium.webdriver.support import expected_conditions as EC
                import pandas as pd
                
                options = Options()
                options.add_argument("start-maximized")
                s = Service('C:\BrowserDrivers\chromedriver.exe')
                driver = webdriver.Chrome(service=s, options=options)
                driver.get("https://www.worldometers.info/coronavirus/")
                data = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#main_table_countries_today"))).get_attribute("outerHTML")
                df  = pd.read_html(data)
                print(df)
                driver.quit()
                

                控制台输出:

                [         # Country,Other  TotalCases  NewCases  ...  Deaths/1M pop   TotalTests  Tests/ 1M pop    Population
                0      NaN         World   264359298  632349.0  ...          673.3          NaN            NaN           NaN
                1      1.0           USA    49662381   89259.0  ...         2415.0  756671013.0      2267182.0  3.337495e+08
                2      2.0         India    34609741    3200.0  ...          336.0  643510926.0       459914.0  1.399198e+09
                3      3.0        Brazil    22118782   12910.0  ...         2865.0   63776166.0       297051.0  2.146975e+08
                4      4.0            UK    10329074   53945.0  ...         2124.0  364875273.0      5335159.0  6.839070e+07
                ..     ...           ...         ...       ...  ...            ...          ...            ...           ...
                221  221.0         Samoa           3       NaN  ...            NaN          NaN            NaN  2.002800e+05
                222  222.0  Saint Helena           2       NaN  ...            NaN          NaN            NaN  6.103000e+03
                223  223.0    Micronesia           1       NaN  ...            NaN          NaN            NaN  1.167290e+05
                224  224.0         Tonga           1       NaN  ...            NaN          NaN            NaN  1.073890e+05
                225    NaN        Total:   264359298  632349.0  ...          673.3          NaN            NaN           NaN
                
                [226 rows x 15 columns]]
                

                这篇关于抓取动态数据硒-无法定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用tkinter创建具有透明背景的标签小部件? 下一篇:如何实现硒刮板的并行运行

                相关文章

              2. <tfoot id='fYKuM'></tfoot>
                1. <legend id='fYKuM'><style id='fYKuM'><dir id='fYKuM'><q id='fYKuM'></q></dir></style></legend>

                    <bdo id='fYKuM'></bdo><ul id='fYKuM'></ul>
                2. <small id='fYKuM'></small><noframes id='fYKuM'>

                  <i id='fYKuM'><tr id='fYKuM'><dt id='fYKuM'><q id='fYKuM'><span id='fYKuM'><b id='fYKuM'><form id='fYKuM'><ins id='fYKuM'></ins><ul id='fYKuM'></ul><sub id='fYKuM'></sub></form><legend id='fYKuM'></legend><bdo id='fYKuM'><pre id='fYKuM'><center id='fYKuM'></center></pre></bdo></b><th id='fYKuM'></th></span></q></dt></tr></i><div id='fYKuM'><tfoot id='fYKuM'></tfoot><dl id='fYKuM'><fieldset id='fYKuM'></fieldset></dl></div>