第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何通過(guò) xpath 或 css 選擇器循環(huán)訪問(wèn)一組類(lèi)

如何通過(guò) xpath 或 css 選擇器循環(huán)訪問(wèn)一組類(lèi)

猛跑小豬 2023-01-04 10:23:14
我想遍歷本網(wǎng)站https://www.dccomics.com/comics中的元素網(wǎng)頁(yè)底部有一個(gè)瀏覽漫畫(huà)的部分,我想從每部漫畫(huà)中抓取名字這是我現(xiàn)在的代碼# importsfrom selenium import webdriverfrom bs4 import BeautifulSoup from selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import By# website urlsbase_url = "https://www.dccomics.com/"comics_url = "https://www.dccomics.com/comics"# Chrome sessiondriver = 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] 這可行,但只獲取結(jié)果標(biāo)題 CSS 類(lèi)的第一個(gè)元素,在本例中為 818。我將如何使用 CSS 選擇器或 Xpath 遍歷每個(gè)結(jié)果標(biāo)題類(lèi)?
查看完整描述

1 回答

?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

使用Selenium 和Python您必須引入WebDriverWait并且visibility_of_all_elements_located()可以使用以下任一定位器策略

使用CSS_SELECTOR:


driver.get('https://www.dccomics.com/comics')

print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.browse-result>a p:not(.result-date)")))])

使用XPATH:


driver.get('https://www.dccomics.com/comics')

print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@class, 'browse-result')]/a//p[not(contains(@class, 'result-date'))]")))])

控制臺(tái)輸出:


['PRIMER', 'DOOMSDAY CLOCK PART 2', 'CATWOMAN #22', 'YOU BROUGHT ME THE OCEAN', 'ACTION COMICS #1022', 'BATMAN/SUPERMAN #9', 'BATMAN: GOTHAM NIGHTS #7', 'BATMAN: THE ADVENTURES CONTINUE #5', 'BIRDS OF PREY #1', 'CATWOMAN 80TH ANNIVERSARY 100-PAGE SUPER SPECTACULAR #1', 'DC GOES TO WAR', "DCEASED: HOPE AT WORLD'S END #2", 'DETECTIVE COMICS #1022', 'FAR SECTOR #6', "HARLEY QUINN: MAKE 'EM LAUGH #1", 'HOUSE OF WHISPERS #21', 'JOHN CONSTANTINE: HELLBLAZER #6', 'JUSTICE LEAGUE DARK #22', 'MARTIAN MANHUNTER: IDENTITY', 'SCOOBY-DOO, WHERE ARE YOU? #104', 'SHAZAM! #12', 'TEEN TITANS GO! TO CAMP #15', 'THE JOKER: 80 YEARS OF THE CLOWN PRINCE OF CRIME THE DELUXE EDITION', 'THE LAST GOD: TALES FROM THE BOOK OF AGES #1', 'THE TERRIFICS VOL. 3: THE GOD GAME']

注意:您必須添加以下導(dǎo)入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


查看完整回答
反對(duì) 回復(fù) 2023-01-04
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)