無頭瀏覽器,也是通過selenium操作瀏覽器,但是瀏覽器不彈出來的意思文章來源:http://www.zghlxwxcb.cn/news/detail-506745.html
需要用到這個:文章來源地址http://www.zghlxwxcb.cn/news/detail-506745.html
from selenium.webdriver.chrome.options import Options ?# 導(dǎo)入無頭瀏覽器的包 opt = Options() opt.add_argument('--headless') ?# 設(shè)置為無頭 opt.add_argument('--disable-gpu') ?# 設(shè)置沒有使用gpu # 1.創(chuàng)建瀏覽器對象 web = Chrome(options= opt) ?# 然后配置放到瀏覽器上
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options # 導(dǎo)入無頭瀏覽器的包
opt = Options()
opt.add_argument('--headless') # 設(shè)置為無頭
opt.add_argument('--disable-gpu') # 設(shè)置沒有使用gpu
# 1.創(chuàng)建瀏覽器對象
web = Chrome(options= opt) # 然后配置放到瀏覽器上
# 2.打開一個網(wǎng)址
web.get('http://lagou.com')
# 定位到地址:北京,然后點擊他
el = web.find_element('xpath','//*[@id="changeCityBox"]/ul/li[1]/a') #這是新版的,舊版是:find_element_by_xpath
el.click() # 點擊事件
# 搜索框輸入:python ,輸入回車/點擊搜索按鈕
web.find_element('xpath','//*[@id="search_input"]').send_keys('python',Keys.ENTER) # Keys.ENTER是回車
div_list = web.find_elements('xpath','//*[@id="jobList"]/div[1]/div')
for div in div_list:
job_name = div.find_element('xpath','./div/div/div/a').text
job_money = div.find_element('xpath','./div/div/div[2]/span').text
company_name = div.find_element('xpath', './div/div[2]/div/a').text
print(job_name,job_money,company_name)
到了這里,關(guān)于selenium無頭瀏覽器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!