爬虫_selenium_Chrome handless

Chrome-headless 模式,Google针对Chrome浏览器59版本新增加的一种模式,可以让你不打开UI界面的情况下使用Chrome浏览器,所以运行效果于Chrome保持完美一致。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

#path是你自己的chrome浏览器的文件路径
path = r'C:\Users\zhang\AppData\Local\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path
browser = webdriver.Chrome(chrome_options = chrome_options)

url = 'https://www.baidu.com'
browser.get(url)
browser.save_screenshot('baidu.png')

 selenium的handless方法封装:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    #path是你自己的chrome浏览器的文件路径
    path = r'C:\Users\zhang\AppData\Local\Google\Chrome\Application\chrome.exe'
    chrome_options.binary_location = path
    browser = webdriver.Chrome(chrome_options=chrome_options)
    return browser

browser=share_browser()
url='https://www.baidu.com'
browser.get(url)

 

 

源代码地址:https://gitee.com/heating-cloud/python_spider.git

posted @ 2022-05-26 16:08  创客未来  阅读(177)  评论(0)    收藏  举报