我想遍历本网站https://www.dccomics.com/comics中的元素
网页底部有一个浏览漫画的部分,我想从每部漫画中抓取名字
这是我现在的代码
# imports
from selenium import webdriver
from bs4 import BeautifulSoup
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
# website urls
base_url = "https://www.dccomics.com/"
comics_url = "https://www.dccomics.com/comics"
# Chrome session
driver = webdriver.Chrome("C:\\laragon\\www\\Proftaak\\chromedriver.exe")
driver.get(comics_url)
driver.implicitly_wait(500)
cookies = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[4]/div[2]/div/button')
driver.execute_script("arguments[0].click();", cookies)
driver.implicitly_wait(100)
clear_filter = driver.find_element_by_class_name('clear-all-action')
driver.execute_script("arguments[0].click();", clear_filter)
array = []
for titles in driver.find_elements_by_class_name('result-title'):
title = titles.find_element_by_xpath('/html/body/div[2]/section/section/div[2]/div/div/div/div/div[3]/div[7]/div[2]/div/div/div/div/div[3]/div[3]/div[2]/div[1]/a/p[1]').text
array.append({'title': title,})
print(array)
driver.quit()
我正在使用下面的 xpath:
/html/body/div[2]/section/section/div[2]/div/div/div/div/div[3]/div[7]/div[2]/div/div/div/div/div[3]/div[3]/div[2]/div[1]/a/p[1]
这可行,但只获取结果标题 CSS 类的第一个元素,在本例中为 818。
我将如何使用 CSS 选择器或 Xpath 遍历每个结果标题类?
喵喵时光机
相关分类