看板 Python 關於我們 聯絡資訊
各位大大晚安,以Xpath抓,一直結果none,請問有可能是哪邊出了問題呢? [in] !pip install selenium from selenium import webdriver import time import pandas as pd browser = webdriver.Chrome(executable_path='./chromedriver.exe') browser.get("https://tw.mall.yahoo.com/store/%E5%B1%88%E8%87%A3%E6%B0%8FWatson s: time.sleep(5) # 商品連結 # 如何找到rel Xpath ? linkpath = '//ul[@class = "sc-eWvPqa cePswM"]/li/a' product_links = browser.find_elements_by_xpath(linkpath) product_link = product_links[0].get_attribute('href') print('商品連結:',product_link) [out] IndexError Traceback (most recent call last) <ipython-input-22-cb447ba73a6b> in <module> 11 linkpath = '//ul[@class = "sc-eWvPqa cePswM"]/li/a' 12 product_links = browser.find_elements_by_xpath(linkpath) ---> 13 product_link = product_links[0].get_attribute('href') 14 print('商品連結:',product_link) IndexError: list index out of range 請問有可能是防爬的原因嗎? 還是哪邊有錯誤嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.25.201.214 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1637410494.A.50D.html ※ 編輯: garlic774 (114.136.86.16 臺灣), 11/21/2021 09:05:52
sating00: 報錯不是很清楚嗎?out of range 就是你給的數超過list 11/21 14:41
sating00: 上限 11/21 14:41
sating00: 先檢查 product_links 到底有沒有東西吧 11/21 14:44
garlic774: 謝謝S大 我再試試看 11/21 19:09
TakiDog: 用chrome debugger 搜尋看看吧 11/21 19:29
TakiDog: 盡可能不要用自動產生的xpath(判斷太嚴格)手動寫判斷不 11/21 19:30
TakiDog: 難 11/21 19:30
garlic774: 後來改成 //a去搜尋,再用if in: 也篩選出來了 感謝大 11/21 22:13
garlic774: 大們 11/21 22:13