【问题标题】:how do i get all the href links in this div我如何获取此 div 中的所有 href 链接
【发布时间】:2021-10-31 19:33:53
【问题描述】:

我正在尝试使用 selenium 在 food.grab.com 上的任何城市的搜索结果页面上获取每家餐厅的链接。这是获取链接所需的 div,但是当我运行它时

rl_elements = driver.find_elements_by_css_selector("ant-layout")

我收到关于未找到 CSS 元素的错误消息。我在想我可以抓住那个 div 然后可能循环或使用 get("href") 标签。这是带有餐厅链接的 div 部分

<div class="ant-layout">
   <div>
      <div class="ant-row-flex RestaurantListRow___1SbZY">
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/mcdonald-s-sta-cruz-church-delivery/AWiD5JwQfYWaYaQC4nY4" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="promoTag___IYhfm">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order McDonald's - Sta Cruz Church" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/AWiD5JwQfYWaYaQC4nY4/hero/20a3b455671746a1b02a2cac627b5a67_1600322490671317355.jpg"></div>
                        <div class="promoTagHead___1bjRG">Promo</div>
                        <div class="promoTagTail___2Jy3D"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">McDonald's - Sta Cruz Church</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">Fast Food, Burgers, American, #Combodeals, #ComboDealsBurgers</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.4
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              29 mins&nbsp;&nbsp;•&nbsp;&nbsp;0.8 km
                           </div>
                        </div>
                     </div>
                     <div class="basicInfoRow___UZM8d discount___3h-0m">
                        <div class="medium___3F_Er discountIcon infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-promo-tag.svg&quot;);"></div>
                        <span class="discountText___GQCkj">P150 OFF: Use code PADALOVE, min 600</span>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/jollibee-raon-delivery/2-CZC1EZEYACBTCX" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order Jollibee - Raon" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/2-CZC1EZEYACBTCX/hero/3189897a1a264165af3c554991970723_1634664559764732889.jpg"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">Jollibee - Raon</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">Filipino, Chicken, Fast Food</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.3
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              33 mins&nbsp;&nbsp;•&nbsp;&nbsp;0.1 km
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/mang-inasal-tayuman-delivery/2-CZCVUA42FBUTTX" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="promoTag___IYhfm">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order Mang Inasal - Tayuman" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/2-CZCVUA42FBUTTX/hero/40d7b143ab2846a490c52bec6b823cfe_1634227146999336525.png"></div>
                        <div class="promoTagHead___1bjRG">Promo</div>
                        <div class="promoTagTail___2Jy3D"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">Mang Inasal - Tayuman</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">Filipino, Chicken, Fast Food</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.5
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              31 mins&nbsp;&nbsp;•&nbsp;&nbsp;2.3 km
                           </div>
                        </div>
                     </div>
                     <div class="basicInfoRow___UZM8d discount___3h-0m">
                        <div class="medium___3F_Er discountIcon infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-promo-tag.svg&quot;);"></div>
                        <span class="discountText___GQCkj">Promo Discount - Paa Large Buddy Size + Pork BBQ Buddy Size</span>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/angel-s-pizza-legarda-available-for-long-distance-delivery-delivery/PHGFSTI000000zw" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order Angel's Pizza - Legarda [Available for LONG-DISTANCE DELIVERY]" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/PHGFSTI000000zw/hero/2166e88cc82b4a8fb3193f1b555002fe_1624797732725799594.jpg"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">Angel's Pizza - Legarda [Available for LONG-DISTANCE DELIVERY]</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">Quick Bites, Pizza, Italian, #ComboDealsPizza</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.6
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              43 mins&nbsp;&nbsp;•&nbsp;&nbsp;1.4 km
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/wendy-s-dapitan-available-for-long-distance-delivery-delivery/PHGFSTI0000017w" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="promoTag___IYhfm">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order Wendy's - Dapitan [Available for LONG-DISTANCE DELIVERY]" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/PHGFSTI0000017w/hero/9cf2d2e2170d4c288706e4ec08740199_1604489462614383246.jpeg"></div>
                        <div class="promoTagHead___1bjRG">Promo</div>
                        <div class="promoTagTail___2Jy3D"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">Wendy's - Dapitan [Available for LONG-DISTANCE DELIVERY]</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">Fast Food, Burgers, American, #ComboDealsBurgers</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.4
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              30 mins&nbsp;&nbsp;•&nbsp;&nbsp;1.5 km
                           </div>
                        </div>
                     </div>
                     <div class="basicInfoRow___UZM8d discount___3h-0m">
                        <div class="medium___3F_Er discountIcon infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-promo-tag.svg&quot;);"></div>
                        <span class="discountText___GQCkj">P150 OFF: Use code PADALOVE, min 600</span>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/kfc-sta-cruz-delivery/2-CYUZC8BTGJ51GJ" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="promoTag___IYhfm">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order KFC - Sta Cruz" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/2-CYUZC8BTGJ51GJ/hero/680a96e5303d4434af6c9ea5751b211e_1593015057999865942.jpeg"></div>
                        <div class="promoTagHead___1bjRG">Promo</div>
                        <div class="promoTagTail___2Jy3D"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">KFC - Sta Cruz</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">American, Chicken, Fast Food</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.1
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              27 mins&nbsp;&nbsp;•&nbsp;&nbsp;0.8 km
                           </div>
                        </div>
                     </div>
                     <div class="basicInfoRow___UZM8d discount___3h-0m">
                        <div class="medium___3F_Er discountIcon infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-promo-tag.svg&quot;);"></div>
                        <span class="discountText___GQCkj">P150 OFF: Use code PADALOVE, min 600</span>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/chowking-sta-cruz-manila-delivery/2-CYMHN7DWAGL2WE" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order Chowking - Sta Cruz Manila" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/2-CYMHN7DWAGL2WE/hero/370cba7acb8645d99e045d93121555cd_1632276886062046084.jpg"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">Chowking - Sta Cruz Manila</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">Chinese, Chicken, Fast Food</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.5
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              31 mins&nbsp;&nbsp;•&nbsp;&nbsp;0.6 km
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </a>
         </div>
         <div class="ant-col-24 RestaurantListCol___1FZ8V  ant-col-md-12 ant-col-lg-6">
            <a href="/ph/en/restaurant/greenwich-binondo-delivery/2-CYKTRUEHNAUGEX" style="color: inherit; text-decoration: none;">
               <div class="ant-row-flex ant-row-flex-start ant-row-flex-top asList___1ZNTr">
                  <div class="ant-col-24 colPhoto___3vb-o ant-col-md-24 ant-col-lg-24">
                     <div class="promoTag___IYhfm">
                        <div class="placeholder___1xbBh restoPhoto___3nncy"><img alt="Order Greenwich - Binondo" class="realImage___2TyNE show___3oA6B" src="https://d1sag4ddilekf6.azureedge.net/compressed/merchants/2-CYKTRUEHNAUGEX/hero/upload-photo-Hero_Photo_66bf96e0fcff47da8b6aacb925f913a3_1554863645049377072.jpeg"></div>
                        <div class="promoTagHead___1bjRG">Promo</div>
                        <div class="promoTagTail___2Jy3D"></div>
                     </div>
                  </div>
                  <div class="ant-col-24 colInfo___3iLqj ant-col-md-24 ant-col-lg-24">
                     <h6 class="name___2epcT">Greenwich - Binondo</h6>
                     <div class="basicInfoContainer___1DcNs">
                        <div class="basicInfoRow___UZM8d cuisine___T2tCh">American, Pizza, Fast Food</div>
                        <div class="basicInfoRow___UZM8d numbers___2xZGn">
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er ratingStar infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-star.svg&quot;);"></div>
                              4.5
                           </div>
                           <div class="numbersChild___2qKMV">
                              <div class="medium___3F_Er deliveryClock infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-clock.svg&quot;);"></div>
                              34 mins&nbsp;&nbsp;•&nbsp;&nbsp;1.6 km
                           </div>
                        </div>
                     </div>
                     <div class="basicInfoRow___UZM8d discount___3h-0m">
                        <div class="medium___3F_Er discountIcon infoItemIcon___23Zvv" role="button" tabindex="0" style="display: inline-block; background-position: center center; background-repeat: no-repeat; background-size: contain; background-image: url(&quot;/static/images/icons/icon-promo-tag.svg&quot;);"></div>
                        <span class="discountText___GQCkj">Lasagna Supreme Flash Sale: two (2) Greenwich Lasagna Supreme Small Pans for only P399!</span>
                     </div>
                  </div>
               </div>
            </a>
         </div>
      </div>
      <button type="button" class="ant-btn ant-btn-block"><span>Load More</span></button>
   </div>
</div>

我目前正在使用它,但它会获取搜索结果页面上的所有链接,我想要上面 div 中的链接。

url_elements = driver.find_elements_by_xpath("//a[contains(@href, '/ph/en/restaurant')]") 
url_elements_list = []
for url_element in url_elements:
    url_elements_list.append(url_element.get_attribute("href"))
sliced_list=url_elements_list[:2]
print(sliced_list)

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    要获取特定 div 元素中的餐厅元素,您应该执行以下操作:

    1. rl_element Web 元素上应用 find_elements_by_xpath 方法,而不是在 Web 驱动程序实例 driver 上应用它。
    2. XPath 定位器应以点. 开头,意思是“从该节点搜索,而不是从整个页面的开头。
    rl_element = driver.find_element_by_class_name("ant-layout")
    url_elements = rl_element.find_elements_by_xpath(".//a[contains(@href, '/ph/en/restaurant')]") 
    url_elements_list = []
    for url_element in url_elements:
        url_elements_list.append(url_element.get_attribute("href"))
    sliced_list=url_elements_list[:2]
    print(sliced_list)
    

    【讨论】:

    • 我收到此错误 AttributeError: 'list' object has no attribute 'find_elements_by_xpath'
    • 当然,抱歉。请参阅我的更新答案。如果确实有多个 rl_elements 元素,您应该调用特定元素,例如 rl_elements[0]
    • @blockhead 编辑后的答案是什么?现在对你有用吗?
    • 是的,它适用于该更改。
    • 很高兴知道?
    【解决方案2】:

    使用那行代码已编辑

    url_elements_list = []
    for url_element in driver.find_elements_by_class_name('ant-layout')::
        url_elements_list.append(url_element.get_attribute("href"))
        print(url_elements_list)
    

    【讨论】:

    • 这将获取页面上所有内容的链接。你刚刚复制了我的代码并发回了。
    • 我尝试了我的自我及其对我的工作。
    • 不可能。里面有很多链接。我不想要那些,我想要我发送的 div 中的那些。
    • 我编辑了代码试试新的。
    • 这是我得到的输出 [无,无,无,无,无,无,无,无,无,无,无,无,无,无]
    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2021-11-26
    • 2020-04-07
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多