1、下载ChromeDriver
通过下面的下载地址,下载对应本地Chrome浏览器版本的ChromeDriver。版本信息可以在 “帮助"=》“关于 Google Chrome” 里查看到。
下载地址:https://registry.npmmirror.com/binary.html?path=chromedriver/
2、安装Selenium
pip install Selenium
3、获取指定标签元素内的html
# -*- encoding: utf-8 -*-
# Created on 2020-09-21 21:54:50
# Project: Selenium
from distutils.file_util import write_file
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time
chrome_options = Options()
chrome_options.add_argument('lang=zh-CN,zh,zh-TW,en-US,en')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
c_service = Service("/usr/bin/chromedriver")#指定下载好的chromedriver的路径
c_service.command_line_args()
c_service.start()
web = webdriver.Chrome(options=chrome_options)
web.get('https://baike.baidu.com/item/%E7%A7%BB%E5%8A%A8%E7%BD%91%E7%BB%9C%E4%BB%A3%E7%A0%81/5935540?fr=aladdin')
time.sleep(5)
#选择指定标签元素
element = web.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]')
page_html = element.get_attribute('innerHTML')
print(page_html)