自学内容网 自学内容网

Python 使用selenium 4.25 进行爬虫(1)

都说python做爬虫比较好,于是我跟着大家的脚步学习python进行爬虫,但是调试了半天,出现各种各样的问题,最终都得到实现了,下面我们来看具体的代码:

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



 
# 如果你没有安装ChromeDriver,需要先下载并配置到环境变量
#driver_path = 'C://chromedriver127.exe'
driver_path = 'C://drivers/chromedriver.exe'   ## 驱动路径
print(webdriver.__version__)   ## 打印selenium的版本
service = Service(driver_path)   ## 创建Serice

# 创建一个 Chrome 浏览器实例
#driver = webdriver.Chrome(executable_path=driver_path)
chrome_options = Options()   ## 定义浏览器选项
chrome_options.add_argument("--headless")  # 无界面模式
 

driver = webdriver.Chrome(service=service,options=chrome_options)  ## 实现驱动


url = "https://www.read8686.com"
driver.get(url)  ##开始爬虫获取

content = driver.page_source  # 获取渲染后的HTML内容
 
# 做你需要的操作,例如解析HTML内容
print(content)
driver.quit()  # 关闭浏览器

执行结果:

希望对你有所帮助


原文地址:https://blog.csdn.net/datouniao1/article/details/142553517

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!