上一篇文章介绍了【python selenium 渲染工具】driver = webdriver.Chrome()报错 这篇文章我们继续学习下selenium 这个自动化工具webdriver的配置和设置
# -*- coding: utf-8 -*-
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import chardet
# url="https://www.phei.com.cn/module/goods/wssd_index.jsp"
url="https://www.ituring.com.cn/"
Chrome_path='D:/Programs/Python/Python3.7/Scripts/chromedriver.exe'
# 配置chrome的参数,设置无界面化
options = Options()
# 设置中文
options.add_argument('lang=zh_CN.UTF-8')
options.add_argument('--headless')
options.add_argument('user-agent="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"')
driver=webdriver.Chrome(executable_path=Chrome_path,options=options)
driver.get(url)
content=driver.page_source
res=content.encode("unicode_escape")
html=str(res,'utf-8')
print(html)
book_name=driver.find_element_by_css_selector('head > meta:nth-child(3)')
book_name=book_name.get_attribute('content')
print(book_name)
|