【问题标题】:Fetch the div element having an attribute with a name获取具有名称属性的 div 元素
【发布时间】:2023-02-25 04:40:19
【问题描述】:

<div class="resultbox_textbox" style="position:relative"><div class="resultbox_title font22 fw500 color111 complist_title" style="min-height:32px;position:relative" title="Nakshatra Hospital in D N Nagar Andheri West, mumbai"><a class="resultbox_title_anchor line_clamp_1 font22 fw500 color111" href="/Mumbai/Nakshatra-Hospital-Andheri-West/022PXX22-XX22-220812130845-C7T1_BZDET?ncatid=10892680&area=&search=%20General%20Physician%20Doctors%20in%20Mumbai&mncatname=General%20Physician%20Doctors" target="_self" title="Nakshatra Hospital in D N Nagar Andheri West, mumbai">Nakshatra Hospital<!-- --> <span class="title_anchor_tag"></span></a><span class="jdicon results_fav_icon" style="position:absolute;right:0;top:0" title="Add to Favorites"></span></div><div class="font12 colorCCC fadeIn animated" style="position:absolute;right:9px;top:34px">P</div><div class="resultbox_overall mt-10" style="min-height:24px" title="Ratings for Nakshatra Hospital in D N Nagar Andheri West, mumbai"><div class="resultbox_totalrate mr-6 font14 fw700 colorFFF" style="min-height:24px">4.5</div><div class="resultbox_starrate"><svg height="100%" viewbox="0 0 1000 200" width="100%"><polygon fill="#FFE372" id="star9927" points="100,10 40,198 190,78 10,78 160,198"></polygon><defs><clippath id="stars9927"><use xlink:href="#star9927"></use><use x="20%" xlink:href="#star9927"></use><use x="40%" xlink:href="#star9927"></use><use x="60%" xlink:href="#star9927"></use><use x="80%" xlink:href="#star9927"></use></clippath></defs><rect clip-path="url(#stars9927)" height="100%" style="fill:rgb(215, 215, 215);stroke:red;stroke-width:1;height:100%;width:100%" width="90%"></rect><rect clip-path="url(#stars9927)" height="100%" style="fill:rgb(255, 110, 0);height:100%" width="90%"></rect></svg></div><div class="resultbox_countrate ml-12 mr-12 font14 fw400 color777" style="min-height:22px">6<!-- --> Ratings</div></div><div class="resultbox_address mt-10" style="min-height:24px"><div class="font15 fw400 color111" style="min-height:23px">D N Nagar Andheri West</div></div><div class="resultbox_amenities mt-10" style="min-height:27px"><span class="amenities_tabs font12 fw500 color777">Cancer Pain Management</span><span class="amenities_tabs font12 fw500 color777">Pulmonology</span><span class="amenities_tabs font12 fw500 color777">Contraception Advice</span><span class="amenities_tabs font12 fw500 color777">Diabetology</span></div><div class="callmn_0 font18 fw500 color111 mt-10" data-did="022PXX22.XX22.220812130845.C7T1" style="min-height:28px"><div><span class="result_call_black jdicon mr-12"></span><span class="cp" id="lessNum_0"><a class="anchor color111" href="tel:07947176733">07947176733</a></span></div></div><div class="resultbox_comment font14 fw400 color777" style="min-height:64px"><div class="reviewcomment_icon mt-15"></div><span class="comment_text mt-10"><q class="comment_text_quote"></q>During the treatment, she makes sure that the patient is comfortable, she never rushes into the treatment, gives enough time to patients to ensure that patients are comfo...<span class="font14 anchor color007 pl-4">more</span></span></div><div class="resultbox_btn_wpr" style="min-height:45px"><div class="resultbox_btnbox mt-10"><div class="dn greenfill_animate callbutton font15 fw500 colorFFF mr-15" id="listing_call_button" style="min-height:35px"><div class="button_flare"></div><span class="callcontent"><span class="whitecall_icon jdicon mr-10"></span>Show Number</span><div class="jsx-d8f990f1402ff3b8 ctatooltip ctatooltip_header ctatooltip_bottom callnow"><div class="jsx-d8f990f1402ff3b8 ctatooltip_textbox_1"><div class="jsx-d8f990f1402ff3b8 font13 fw400 colorFFF mt-5 nowrap">Click here to see the number</div><div class="jsx-d8f990f1402ff3b8 ctatooltip_btnbox ml-20"><button class="jsx-d8f990f1402ff3b8 ctatooltip_buttton filled font14 fw400 color111">OK</button></div></div></div></div><button class="blue_whitefill_animate onlinebtn font15 fw500 mr-15" style="min-height:35px">Send Enquiry</button></div><div class="resultbox_btnbox mt-10"></div></div></div>

以上是 div 列表中的一个 div。上面的一个 div 具有属性 data-did 。我有上面 div 的句柄,但是我如何才能获取这个具有名称为data-did 的属性的 div

这是我为解析 URL 的内容而编写的当前代码:

driver = webdriver.Chrome()   
driver.get("url") 
html = driver.page_source    
soup = BeautifulSoup(html, "html.parser")  
ds= soup.find_all("div", class_="rt")  
for d in ds:
     d. # do something on d to fetch the div with attribute data-did

【问题讨论】:

    标签: python-3.x selenium-webdriver beautifulsoup selenium-chromedriver


    【解决方案1】:

    使用以下任一相关 XPath 表达式:

    //div[@data-did]
    

    或者:

    //div/@data-did
    

    或者:

    //*/@data-did
    

    【讨论】:

      【解决方案2】:

      如果您想识别属性名称为data-diddiv元素列表,您可以使用任何定位器策略。

      CSS 选择器:

      driver.find_elements(By.CSS_SELECTOR,"div[data-did]")
      

      路径:

      driver.find_elements(By.XPATH,"//div[@data-did]")
      

      理想情况下处理动态元素使用 webdriverwait()

      WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"div[data-did]")))
      

      或者

      WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@data-did]")))
      

      你需要导入下面的库

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

      更新:

      如果你使用 soup,那么使用下面的 css 选择器。

      driver = webdriver.Chrome()   
      driver.get("url") 
      html = driver.page_source    
      soup = BeautifulSoup(html, "html.parser") 
      ds= soup.select("div[data-did]")  
      for d in ds:
          print(d.text)
          print(d['data-did'])
      

      【讨论】:

      • 我已经用我已有的一些代码更新了 OP。我不确定如何在代码中使用变量 d 来使用您的方法。所以 d = div 贴在上面
      • @juan,检查我更新的答案,让我知道情况如何?
      • 这给了我一个错误。尝试替换 url = https://www.justdial.com/Mumbai/General-Physician-Doctors/nct-10892680 这是我正在使用并试图获取姓名、电话号码和地址的确切 URL
      【解决方案3】:

      定位具有属性的<div>元素data-did您可以使用以下任一locator strategies

      • 使用CSS_SELECTOR:

        element = driver.find_element(By.CSS_SELECTOR, "div[data-did]")
        
      • 使用XPATH:

        element = driver.find_element(By.XPATH, "//div[@data-did]")
        

      要理想地定位元素,您需要为 visibility_of_element_located() 引入 WebDriverWait,您可以使用以下任一 locator strategies

      • 使用CSS_SELECTOR:

        element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-did]")))
        
      • 使用XPATH:

        element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-did]")))
        
      • 笔记:您必须添加以下导入:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-06
        相关资源
        最近更新 更多