无法在网络刮板中打印HREF,没有错误

发布于 2025-02-11 05:11:10 字数 1525 浏览 1 评论 0原文

我能够从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2025-02-18 05:11:10

您无法使用CSS选择器访问属性。

我将尝试首先将HREF更改为a

href_elements = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline a")

You can't access an attribute using a css selector.

I'd try first to change href to a:

href_elements = driver.find_elements(By.CSS_SELECTOR, ".search-list p.card-body__headline a")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文