# -*- coding: UTF-8 -*-
from selenium import webdriver
from bs4 import BeautifulSoup
import time
#这里我已经下载了geckodriver放在火狐文件夹,如果放入所需要执行的Python脚本的所在处就不用写这个了
myDriver = webdriver.Firefox(executable_path ='F:\setup place\Firefox\geckodriver.exe')
myDriver.get('http://www.heibanke.com/accounts/login')
time.sleep(3)
myDriver.find_element_by_id('id_username').send_keys('test')
myDriver.find_element_by_id('id_password').send_keys('test123')
myDriver.find_element_by_id('id_submit').click()
time.sleep(1)
number = 0
#被重定向后我们就重新写入要进入的URL
while number<31:
myDriver.get('http://www.heibanke.com/lesson/crawler_ex02/')
time.sleep(2)
myDriver.find_element_by_name('username').send_keys('test')
myDriver.find_element_by_name('password').send_keys(str(number))
myDriver.find_element_by_id('id_submit').click()
time.sleep(2)
html = myDriver.page_source
bs_obj = BeautifulSoup(html,"html.parser")
print bs_obj.h3
if bs_obj.text.find(u'密码错误')>0:
number = number + 1
print number
else:
print bs_obj.text
break
time.sleep(2)
myDriver.close()
复制代码
利用selenium爬取重定向内容
最新推荐文章于 2024-09-06 08:45:16 发布