链接的HTTP选择器(xpath或css)

2024-09-30 08:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取此站点中每只鞋的href元素:

http://www.soccerpro.com/Clearance-Soccer-Shoes-c168/

但我找不到合适的选择器。你知道吗

response.xpath('.//*[@class="newnav itemnamelink"]')
[]

有人知道我如何在xpath或css中做到这一点吗?你知道吗


Tags: comhttp元素站点responsewww选择器xpath
1条回答
网友
1楼 · 发布于 2024-09-30 08:14:58

所需的链接是动态生成的,因此您无法从HTML源中获取它们,就像requests.get("http://www.soccerpro.com/Clearance-Soccer-Shoes-c168/")

您可以使用selenium通过浏览器会话获取所需的值:

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

driver = web.Chrome()
driver.get('http://www.soccerpro.com/Clearance-Soccer-Shoes-c168/')
wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//table[@class='getproductdisplay-innertable']")))
links = [link.get_attribute('href') for link in driver.find_elements_by_xpath('//a[@class="newnav itemnamelink"]')]

相关问题 更多 >

    热门问题