Python:使用selenium查找所有特定标记

时间:2018-05-10 08:03:05

标签: python selenium web-scraping selenium-chromedriver

HTML:

<div id="searchResult">
  <div class="buySearchResultContent">
    <div class="buySearchResultContentImg">
      <a href="carinfo-333285.php">
        <img src="carpics/9400180056/290x200/20180305101502854_4567823.jpg" srcset="carpics/9400180056/290x200/20180305101502854_9098765.jpg 290w, carpics/9400180056/435x300/20180305101502854_00000.jpg 435w , carpics/9400180056/720x520/20180305101502854_00001.jpg 720w" sizes="(min-width: 992px) 75vw, 90vw" alt="auto">
       </a>
     </div>
     <div class="buySearchResultContentImg">
       <a href="carinfo-333286.php">
         <img src="carpics/9400180056/290x200/20180305101502854_4567824.jpg" srcset="carpics/9400180056/290x200/20180305101502854_9098766.jpg 290w, carpics/9400180056/435x300/20180305101502854_00000.jpg 436w , carpics/9400180056/720x520/20180305101502854_00001.jpg 721w" sizes="(min-width: 992px) 75vw, 90vw" alt="auto">
        </a>
      </div>
    </div>
</div>

我想要做的是提取两个href,但是使用我的代码,我只能提取第一个。

代码:

driver.find_element_by_css_selector("buySearchResultContentImg > div").get_attribute("href")

1 个答案:

答案 0 :(得分:3)

尝试使用以下代码获取@href值列表:

links = [link.get_attribute("href") for link in driver.find_elements_by_css_selector(".buySearchResultContentImg>a")]