Python Selenium - 获取 href 值
Python Selenium - get href value
我正在尝试从网站复制 href 值,html 代码如下所示:
<p class="sc-eYdvao kvdWiq">
<a href="https://www.iproperty.com.my/property/setia-eco-park/sale-
1653165/">Shah Alam Setia Eco Park, Setia Eco Park
</a>
</p>
我试过 driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href")
但它返回 'list' object has no attribute 'get_attribute'
。使用 driver.find_element_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href")
返回 None
。但我不能使用 xpath,因为该网站有 20 多个 href,我需要将其全部复制。使用 xpath 只会复制一个。
如果有帮助,所有 20+ href 都归类在相同的 class 下,即 sc-eYdvao kvdWiq
。
最终我想复制所有 20+ href 并将它们导出到 csv 文件。
感谢任何可能的帮助。
试试这样的东西:
elems = driver.find_elements_by_xpath("//p[contains(@class, 'sc-eYdvao') and contains(@class='kvdWiq')]/a")
for elem in elems:
print elem.get_attribute['href']
XPATH
//p[@class='sc-eYdvao kvdWiq']/a
return你要找的元素。
将数据写入 CSV 文件与抓取挑战无关。看例子就可以了
如果有多个元素,您需要 driver.find_elements。这将 return 一个列表。对于 css 选择器,您要确保选择的是那些 类 具有子 href
的选择器
elems = driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq [href]")
links = [elem.get_attribute('href') for elem in elems]
您可能还需要一个等待条件,以等待 css 选择器定位的所有元素。
elems = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".sc-eYdvao.kvdWiq [href]")))
根据给定的 HTML:
<p class="sc-eYdvao kvdWiq">
<a href="https://www.iproperty.com.my/property/setia-eco-park/sale-1653165/">Shah Alam Setia Eco Park, Setia Eco Park</a>
</p>
由于 href
属性在 <a>
标签内,理想情况下您需要更深入地移动到 <a>
节点。因此,要提取 href
属性 的值,您可以使用以下任一方法 :
使用css_selector
:
print(driver.find_element_by_css_selector("p.sc-eYdvao.kvdWiq > a").get_attribute('href'))
使用xpath
:
print(driver.find_element_by_xpath("//p[@class='sc-eYdvao kvdWiq']/a").get_attribute('href'))
如果您想提取 href
属性的所有值 您需要使用 find_elements*
代替:
使用css_selector
:
print([my_elem.get_attribute("href") for my_elem in driver.find_elements_by_css_selector("p.sc-eYdvao.kvdWiq > a")])
使用xpath
:
print([my_elem.get_attribute("href") for my_elem in driver.find_elements_by_xpath("//p[@class='sc-eYdvao kvdWiq']/a")])
动态元素
但是,如果您观察 class 属性的 值,即 sc-eYdvao
和 kvdWiq
理想情况下,这些是 动态 值。因此,要提取 href
属性,您必须为 visibility_of_element_located()
引入 并且您可以使用以下任一 Locator Strategies:
使用CSS_SELECTOR
:
print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p.sc-eYdvao.kvdWiq > a"))).get_attribute('href'))
使用XPATH
:
print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//p[@class='sc-eYdvao kvdWiq']/a"))).get_attribute('href'))
如果要提取 href
属性 的所有值,可以使用 visibility_of_all_elements_located()
代替:
使用CSS_SELECTOR
:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "p.sc-eYdvao.kvdWiq > a")))])
使用XPATH
:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//p[@class='sc-eYdvao kvdWiq']/a")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
要抓取任何 hyperlink 或 Href,proxycrwal API 是理想的选择,因为它使用预构建的函数来获取所需的信息。只需 pip install API 并按照代码获取所需的输出。使用 python selenium 获取 Href links 的第二种方法是 运行 以下代码。
Source Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
list = ['https://www.heliosholland.com/Ampullendoos-voor-63-ampullen','https://www.heliosholland.com/lege-testdozen’]
driver = webdriver.Chrome()
wait = WebDriverWait(driver,29)
for i in list:
driver.get(i)
image = wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[1]/div[3]/div[2]/div/div[2]/div/div/form/div[1]/div[1]/div/div/div/div[1]/div/img'))).get_attribute('src')
print(image)
要抓取 link,请使用 .get_attribute(‘src’).
我正在尝试从网站复制 href 值,html 代码如下所示:
<p class="sc-eYdvao kvdWiq">
<a href="https://www.iproperty.com.my/property/setia-eco-park/sale-
1653165/">Shah Alam Setia Eco Park, Setia Eco Park
</a>
</p>
我试过 driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href")
但它返回 'list' object has no attribute 'get_attribute'
。使用 driver.find_element_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href")
返回 None
。但我不能使用 xpath,因为该网站有 20 多个 href,我需要将其全部复制。使用 xpath 只会复制一个。
如果有帮助,所有 20+ href 都归类在相同的 class 下,即 sc-eYdvao kvdWiq
。
最终我想复制所有 20+ href 并将它们导出到 csv 文件。
感谢任何可能的帮助。
试试这样的东西:
elems = driver.find_elements_by_xpath("//p[contains(@class, 'sc-eYdvao') and contains(@class='kvdWiq')]/a")
for elem in elems:
print elem.get_attribute['href']
XPATH
//p[@class='sc-eYdvao kvdWiq']/a
return你要找的元素。
将数据写入 CSV 文件与抓取挑战无关。看例子就可以了
如果有多个元素,您需要 driver.find_elements。这将 return 一个列表。对于 css 选择器,您要确保选择的是那些 类 具有子 href
的选择器elems = driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq [href]")
links = [elem.get_attribute('href') for elem in elems]
您可能还需要一个等待条件,以等待 css 选择器定位的所有元素。
elems = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".sc-eYdvao.kvdWiq [href]")))
根据给定的 HTML:
<p class="sc-eYdvao kvdWiq">
<a href="https://www.iproperty.com.my/property/setia-eco-park/sale-1653165/">Shah Alam Setia Eco Park, Setia Eco Park</a>
</p>
由于 href
属性在 <a>
标签内,理想情况下您需要更深入地移动到 <a>
节点。因此,要提取 href
属性 的值,您可以使用以下任一方法
使用
css_selector
:print(driver.find_element_by_css_selector("p.sc-eYdvao.kvdWiq > a").get_attribute('href'))
使用
xpath
:print(driver.find_element_by_xpath("//p[@class='sc-eYdvao kvdWiq']/a").get_attribute('href'))
如果您想提取 href
属性的所有值 您需要使用 find_elements*
代替:
使用
css_selector
:print([my_elem.get_attribute("href") for my_elem in driver.find_elements_by_css_selector("p.sc-eYdvao.kvdWiq > a")])
使用
xpath
:print([my_elem.get_attribute("href") for my_elem in driver.find_elements_by_xpath("//p[@class='sc-eYdvao kvdWiq']/a")])
动态元素
但是,如果您观察 class 属性的 值,即 sc-eYdvao
和 kvdWiq
理想情况下,这些是 动态 值。因此,要提取 href
属性,您必须为 visibility_of_element_located()
引入
使用
CSS_SELECTOR
:print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p.sc-eYdvao.kvdWiq > a"))).get_attribute('href'))
使用
XPATH
:print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//p[@class='sc-eYdvao kvdWiq']/a"))).get_attribute('href'))
如果要提取 href
属性 的所有值,可以使用 visibility_of_all_elements_located()
代替:
使用
CSS_SELECTOR
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "p.sc-eYdvao.kvdWiq > a")))])
使用
XPATH
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//p[@class='sc-eYdvao kvdWiq']/a")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
要抓取任何 hyperlink 或 Href,proxycrwal API 是理想的选择,因为它使用预构建的函数来获取所需的信息。只需 pip install API 并按照代码获取所需的输出。使用 python selenium 获取 Href links 的第二种方法是 运行 以下代码。
Source Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
list = ['https://www.heliosholland.com/Ampullendoos-voor-63-ampullen','https://www.heliosholland.com/lege-testdozen’]
driver = webdriver.Chrome()
wait = WebDriverWait(driver,29)
for i in list:
driver.get(i)
image = wait.until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[1]/div[3]/div[2]/div/div[2]/div/div/form/div[1]/div[1]/div/div/div/div[1]/div/img'))).get_attribute('src')
print(image)
要抓取 link,请使用 .get_attribute(‘src’).