[問題] Selenium xpath 讀取href的問題

看板Python作者 (超帥肥宅)時間6年前 (2018/03/11 17:20), 6年前編輯推噓2(201)
留言3則, 2人參與, 6年前最新討論串1/1
哈囉,我正在用Python寫一個自動抓圖程式,想要從HTML碼中抓出href的連結。 使用Selenium 的 find_element_by_xpath ,但不管怎麼樣都抓不出連結。 HTML碼如下: ... <li><a class="AAA XXX" href="連結1">View</a></li> <li><a class="AAA" href="連結1">Download</a></li> <li><a class="BBB" href="連結2">Download Large Version</a></li> ... 程式碼如下: try : # 優先下載大圖(連結2) target = driver.find_element_by_xpath("//a[@class='BBB']") print(target.text) except: # 沒有大圖 try: # 下載小圖 (連結1) target = driver.find_element_by_xpath("//a[@class='AAA']") print(target.text) except: # 連結沒有圖片 print ("nothing") 輸出結果: Download Large Version 上網查了一下,改成 target = driver.find_element_by_xpath("//a[@class='BBB']/@href") 輸出結果則是: nothing 請問一下,要怎麼讓xpath讀出href的連結呢? 還是有什麼更好的方法呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.81.77 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1520760050.A.57C.html 我還是用 Selenium -> BeautifulSoup4 -> Regular Expression 這種方法好了... ※ 編輯: zo6596001 (114.45.81.77), 03/11/2018 18:32:50

03/12 01:01, 6年前 , 1F
原本做法改成print(target.get_attribute('xref')) 這樣?
03/12 01:01, 1F

03/12 01:01, 6年前 , 2F
h
03/12 01:01, 2F

03/12 08:49, 6年前 , 3F
.get_attribute 抓標籤內都是這lol
03/12 08:49, 3F
文章代碼(AID): #1QfFJoLy (Python)