无法在网络刮板中打印HREF,没有错误
我能够从HTML中提取名称,但是试图获得HREF链接并没有解决。我需要获取它,以便它也打印出附加名称旁边的链接。在做到这一点之前,我需要能够首先打印链接。
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
user_input = input("character: ")
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
driver.get("https://www.marvel.com/search")
search_input_xpath = "//input[@placeholder='Search']"
search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, search_input_xpath)))
search_input.send_keys(user_input)
first_item_in_auto_suggest_area_xpath = "//div[contains(@id,'react-autowhatever')]/ul"
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, first_item_in_auto_suggest_area_xpath)))
search_input.send_keys(Keys.ENTER)
section = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".search-list p.card-body__headline a")))
character = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline a")
for character_tag in character:
print(character_tag.text)
href_elements = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline href")
for href in href_elements:
print(href.get_attribute("href"))
I am able to extract the names from the html however trying to get the href links isn't working out. I am needing to get it so it prints the link next to the name its attached too. before I can do that I need to be able to print the link in the first place.
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
user_input = input("character: ")
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
driver.get("https://www.marvel.com/search")
search_input_xpath = "//input[@placeholder='Search']"
search_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, search_input_xpath)))
search_input.send_keys(user_input)
first_item_in_auto_suggest_area_xpath = "//div[contains(@id,'react-autowhatever')]/ul"
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, first_item_in_auto_suggest_area_xpath)))
search_input.send_keys(Keys.ENTER)
section = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".search-list p.card-body__headline a")))
character = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline a")
for character_tag in character:
print(character_tag.text)
href_elements = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline href")
for href in href_elements:
print(href.get_attribute("href"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用CSS选择器访问属性。
我将尝试首先将
HREF
更改为a
:You can't access an attribute using a css selector.
I'd try first to change
href
toa
: