返回顶部

zhangfd

个人博客,仅供学习使用

导航

selenium账号密码模拟登陆豆瓣

install

pip install selenium

drivers

安装各浏览器的驱动
当然我们是通过各浏览器的驱动程序 来操作浏览器的,所以,还要有各浏览器的驱动程序。
我们主要以谷歌的chrome浏览器为例来演示。

chrome driver

chrom浏览器的web driver(chromedriver.exe),可以在下面网址访问:
http://npm.taobao.org/mirrors/chromedriver/

firefox driver

firefox(火狐浏览器)的web driver (geckodriver.exe)在这里访问:
https://github.com/mozilla/geckodriver/releases

other drivers

其他浏览器驱动可以见下面列表:
Edge:https://developer.microsoft.com/en-us/micrsosft-edage/tools/webdriver
Safari:https://webkit.org/blog/6900/webdriver-support-in-safari-10/

geckodriver

selenium3.0启动firefox需要geckodriver.exe这个驱动文件。
下载地址:https://github.com/mozilla/geckodriver/releases
下载之后,配置到环境变量path下(可以直接放python根目录)

how to login

以登录豆瓣为例
1、打开浏览器
2、打开网址
3、点击密码登录
4、定位账号和密码
5、输入内容
6、点击登录豆瓣

codes

from django.shortcuts import render
from selenium import webdriver
import time 
from setting import name,passwd

# Create your views here.
# 以登录豆瓣为例
# 1、打开浏览器
driver = webdriver.Chrome('E:\技术学习\Python+爬虫\projects\moni_login\software\chromedriver.exe')
# 2、打开网址
driver.get("https://accounts.douban.com/passport/login")
# driver.get("https://www.douban.com/")
# 3、点击定位"密码登录" ,注意是:find_element_by_class_name,注意是单引号
driver.find_element_by_class_name('account-tab-account').click()
 
# 4、定位账号和密码、输入内容
driver.find_element_by_id('username').send_keys(name)
driver.find_element_by_id('password').send_keys(passwd)
# 6、点击登录豆瓣
driver.find_element_by_partial_link_text('登录豆瓣').click()

# 获取网页代码
html = driver.page_source
print(html)
time.sleep(5)
driver.quit()

setting.py

name = "*********"
passwd = "********"

posted on 2020-08-23 22:55  zhangfd  阅读(302)  评论(0编辑  收藏  举报