Python:通过href查找元素

时间:2018-05-10 06:04:44

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

我使用webdriver Chrome从网站上抓取数据,但我不知道如何从href中提取数据。

HTM:

<div class="buySearchResultContent">
  <ul id="CARS_LIST_DATA">
      <li class="seo_list" data-seo_name="440285">
        <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>

我的代码:

driver = webdriver.Chrome("C:/chromedriver.exe")
url = "https://www.asdf.com.tw/price-02.php?v=3&brand=lisa&model=lulu&year1=2009&year2=2018&page=1"
driver.get(url)
content=driver.find_element_by_class_name('buySearchResultContentImg')
print(content)

我想要提取的是“carinfo-333285.php”。谢谢!

1 个答案:

答案 0 :(得分:0)

我不太了解python,请尝试这个

Jpg_href= driver.find_element_by_xpath("//div[@class='buySearchResultContentImg']/a[@href='carinfo-333285.php']").get_attribute("href")