我是硒的新手。我想從網站的以下 HTML 代碼中提取 HTML 表中的鏈接文本。對于上面的代碼片段,我在 selenium 中編寫了以下代碼:from selenium import webdriverfrom selenium.webdriver.common.by import Byoption = webdriver.ChromeOptions()option.add_argument("--incognito")option.add_argument("--start-maximized")chrome_path = r"C:\Users\singh\Downloads\chromedriver_win32\chromedriver.exe"browser = webdriver.Chrome(chrome_path, options=option)browser.get("https://timesofindia.indiatimes.com/archive/year-2010,month-1.cms")browser.implicitly_wait(10)num = []numbers = browser.find_elements(By.XPATH, "//table[@class = 'calender']/tbody/tr/td/a[@href]")for n in numbers: number = n.text num.append(number)預期輸出:num = ['S', 'M', 'T', 'W', 'T', 'F', 'S', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31']該程序將num和numbers列表作為空列表返回。我知道在代碼中輸入的 XPath 中有些地方不正確。但我無法弄清楚錯誤是什么。操作系統:Windows 10 x64Python IDE:Anaconda SpyderPython 版本:3.6
1 回答

慕標5832272
TA貢獻1966條經驗 獲得超4個贊
有兩個問題
1.calender是id,不是class。
2.為了得到href你需要使用的get_attribute,而不是文本
numbers = browser.find_elements(By.XPATH, '//table[@id="calender"]//a')
for n in numbers:
number = n.get_attribute('href')
num.append(number)
添加回答
舉報
0/150
提交
取消