Re: Selenium爬取資料出錯

看板Python作者時間4年前 (2021/02/11 20:20), 4年前編輯推噓5(5026)
留言31則, 2人參與, 4年前最新討論串2/2 (看更多)
作者 chickengod (雞大俠) 標題 Re: Selenium爬取資料出錯 時間 Thu Feb 11 20:20:09 2021 ─────────────────────────────────────── ※ 引述《code0093 (DADY)》之銘言: : 我要爬工商名錄的資料 : 網址 : https://dmz26.moea.gov.tw/GMWeb/investigate/InvestigateFactory.aspx : 程式碼貼在GitHub : https://github.com/san60708/helppppppp/blob/main/ss : 不知道為什麼我用find_elements找到資料後再用for把資料抓出來。他只會抓第一行... : 不知道哪裡出錯了 : 請大大協助,感謝~ 你需要解決網頁的彈出視窗 https://i.imgur.com/cSgaX12.jpeg
不然下一次的 driver.get() 好像會因為這樣被卡住 然後就一直死在 exception 的 while loop 以下的程式可以在遇到 alert 彈出視窗 exception 的時候按確定 from selenium.common.exceptions import UnexpectedAlertPresentException # 此處用來取代原本的 except except UnexpectedAlertPresentException as ex: print("Exception has been thrown. " + str(ex)) alert = driver.switch_to.alert alert.accept() """ Exception has been thrown. Alert Text: None Message: unexpected alert open: {Alert text : 查無資料} """ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.25.137.58 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1613046017.A.FB6.html ※ 編輯: chickengod (114.25.137.58 臺灣), 02/12/2021 04:03:31

02/12 17:32, 4年前 , 1F
感謝大大回覆
02/12 17:32, 1F

02/12 17:33, 4年前 , 2F
我先測試某一頁,暫且忽略跳出的方塊
02/12 17:33, 2F

02/12 17:33, 4年前 , 3F

02/12 17:34, 4年前 , 4F

02/12 17:34, 4年前 , 5F
程式26行開始爬取資料
02/12 17:34, 5F

02/12 17:34, 4年前 , 6F

02/12 17:35, 4年前 , 7F
程式只爬得出第一行...然後這個頁面總共683筆,他就重
02/12 17:35, 7F

02/12 17:35, 4年前 , 8F
複爬683次
02/12 17:35, 8F
Hi 根據 selenium 文件 https://tinyurl.com/2de8zwvf find_elements_by_xpath() return "WebElement" list https://tinyurl.com/15na3djs WebElement 是一個 class 你可以使用內建的 property 比如 .text 得到文字 data = driver.find_elements_by_xpath("//div//table[@width='90%']/tbody") print(data[0].text) print(data[1].text) 類似這樣 """ 工廠登記編號 630209 ... 工廠登記編號 630206 ... """ ※ 編輯: chickengod (114.25.137.58 臺灣), 02/12/2021 23:22:04

02/14 00:37, 4年前 , 9F
感謝大大。但是我現在用for單純印出data.text是正常的
02/14 00:37, 9F

02/14 00:37, 4年前 , 10F
但是如果data.find就又是錯誤的 如圖
02/14 00:37, 10F

02/14 00:38, 4年前 , 11F

02/14 00:38, 4年前 , 12F

02/14 00:39, 4年前 , 13F
如果是webelement的話 那我要再更深入超多特定資料為什
02/14 00:39, 13F

02/14 00:39, 4年前 , 14F
麼會只印出第一筆呢..還是是我電腦問題
02/14 00:39, 14F

02/14 00:39, 4年前 , 15F
感謝大大協助
02/14 00:39, 15F

02/14 00:47, 4年前 , 16F

02/14 00:49, 4年前 , 17F
我換另一種方法寫就沒問題了,但是我還是很疑惑為何用fo
02/14 00:49, 17F

02/14 00:49, 4年前 , 18F
r不行...
02/14 00:49, 18F

02/14 02:39, 4年前 , 19F
我猜可能跟這個有關?
02/14 02:39, 19F

02/14 02:39, 4年前 , 20F

02/14 02:39, 4年前 , 21F
會根據 xpath 寫法選擇抓整頁或者相對於元素的位置
02/14 02:39, 21F

02/14 02:51, 4年前 , 22F
如果都是長的像絕對路徑的xpath
02/14 02:51, 22F

02/14 02:51, 4年前 , 23F
大概就不管目前元素在哪 一律抓第一個
02/14 02:51, 23F

02/14 02:51, 4年前 , 24F
也許就能解釋為什麼你會遇到這情況
02/14 02:51, 24F

02/14 02:51, 4年前 , 25F
不過我是覺得以都能達到目的來說
02/14 02:51, 25F

02/14 02:51, 4年前 , 26F
直接抓 .text 存下來
02/14 02:51, 26F

02/14 02:51, 4年前 , 27F
再一次針對全部字串處理會不會執行比較快
02/14 02:51, 27F

02/14 02:51, 4年前 , 28F
畢竟 selenium 還要控制一個瀏覽器 多少會影響效能
02/14 02:51, 28F

02/14 22:50, 4年前 , 29F
了解,感謝大大的用心回答
02/14 22:50, 29F

02/14 22:51, 4年前 , 30F
目前我就要先抓text下來再分解字串 效率確實快很多
02/14 22:51, 30F

02/14 23:41, 4年前 , 31F
( ̄▽ ̄) okder ~
02/14 23:41, 31F
文章代碼(AID): #1W9I41-s (Python)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1W9I41-s (Python)