python3里的selenium模块可以对浏览器进行自动化操作,这次来实现对微博的自动化登录,代码如下:
from selenium import webdriver
from configparser import ConfigParser
#第一步,打开浏览器
wb = webdriver.Chrome()
# 设置浏览器反应时间
wb.implicitly_wait(10)
#设置想要的窗口的大小
wb.set_window_size(1000,800)
#设置访问的网站
url = 'https://weibo.com/'
wb.get(url)
#输入相关信息
wb.find_element_by_id('loginname').send_keys('12345678') #账号
# 对账号的密码进行保护
target = ConfigParser()
target.read('pwd.ini',encoding= 'utf-8')
pwd = target.get('weibo','password')
#隐藏自己的密码 #点击登陆 wb.find_element_by_xpath("http://div[@ class='info_list login_btn']/a").click() 这里博主用的是谷歌浏览器(chromedriver.exe),读者要根据自己的谷歌浏览器版本选择合适的驱动版本,再将chromedriver.exe加入环境变量就可以使用。 chromedriver.exe各版本下载地址:https://www.pop1314.com 代码到这里就结束了,这次代码量比较简单,读者有不懂的地方可以私信博主或在下方进行评论,博主会在第一时间进行答复,我是活动的笑脸。
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章