爬蟲selenium解決網頁空白問題

from selenium.webdriver import Chrome import time # https://blog.csdn.net/zhoukeguai/article/details/113247342 # driver = Chrome("./chromedriver.exe") # 把當前目錄下的chromdriver.exe複製到你的python安裝目錄. 我用的124版本, 根據自己需要下載即可. from selenium import webdriver from selenium.webdriver.chrome.options import Options # 導入無頭瀏覽器的包 opt = Options() opt.add_argument('--headless') # 設置爲無頭 # opt.add_argument('--disable-gpu') # 設置沒有使用gpu opt.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36') user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" opt.add_argument(f'user-agent={user_agent}') opt.add_experimental_option("excludeSwitches",["enable-automation"]) opt.add_experimental_option("useAutomationExtension",'False') opt.add_argument('--disable-javascript') opt.add_argument('disable-infobars') opt.add_experimental_option('detach', True) opt.add_argument('-disable-blink-features=AutomationControlled') # =====這個可以解決網頁空白的問題. web = Chrome(options= opt) # 然後配置放到瀏覽器上 web.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) # http://www.chinadrugtrials.org.cn/clinicaltrials.searchlistdetail.dhtml web.get('http://www.chinadrugtrials.org.cn/clinicaltrials.searchlistdetail.dhtml')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章