Re: Selenium爬取資料出錯
作者 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:33, 3F

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

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

→
02/12 17:35,
4年前
, 7F
02/12 17:35, 7F
→
02/12 17:35,
4年前
, 8F
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
02/14 00:37, 9F
→
02/14 00:37,
4年前
, 10F
02/14 00:37, 10F
→
02/14 00:38,
4年前
, 11F
02/14 00:38, 11F

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

→
02/14 00:39,
4年前
, 13F
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:47, 16F

推
02/14 00:49,
4年前
, 17F
02/14 00:49, 17F
→
02/14 00:49,
4年前
, 18F
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, 20F

→
02/14 02:39,
4年前
, 21F
02/14 02:39, 21F
→
02/14 02:51,
4年前
, 22F
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
02/14 02:51, 26F
→
02/14 02:51,
4年前
, 27F
02/14 02:51, 27F
→
02/14 02:51,
4年前
, 28F
02/14 02:51, 28F
推
02/14 22:50,
4年前
, 29F
02/14 22:50, 29F
→
02/14 22:51,
4年前
, 30F
02/14 22:51, 30F
→
02/14 23:41,
4年前
, 31F
02/14 23:41, 31F
討論串 (同標題文章)
Python 近期熱門文章
PTT數位生活區 即時熱門文章