第五章 爬虫进阶(二十六) 2020-02-12

二十六、 selenium实战– 登录功能实现

 

续上例分解,示例代码:


from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

 

 

# 为什么需要把driver放在外面?

# 因为如果放在里面,那么driver将会随着对象的销毁而被销毁

# 而我们的TrainSpider的对象是放在main函数中执行的。

# 只要main函数运行完成后,里面所有的变量都会被销毁,

# 也就是说spider也会被销毁,那么spider里面的driver也会被销毁。

 

driver = webdriver.Chrome(executable_path="E:\python\chromedriver\chromedriver.exe")

 

 

class TrainSpider(object):

    login_url = "https://kyfw.12306.cn/otn/resources/login.html"

    personal_url ="https://kyfw.12306.cn/otn/view/index.html"

 

    def __init__(self, from_station,to_station, train_date):

        self.from_station = from_station

        self.to_station = to_station

        self.train_date = train_date

 

    def login(self):

        driver.get(self.login_url)

       # 等待url是否变成个人中心的url,来判断是否登录成功

        WebDriverWait(driver, 1000).until(

            EC.url_contains(self.personal_url)

        )

        print("登录成功!")

 

    def run(self):

       # 1、登录

        self.login()

 

 

def main():

    spider = TrainSpider("北京", "长沙","2020-02-12")

    spider.run()

 

 

if__name__ == '__main__':

    main()



上一篇文章 第五章 爬虫进阶(二十五) 2020-02-11 地址:

https://www.jianshu.com/p/7d0b0afebf15

下一篇文章 第五章 爬虫进阶(二十七) 2020-02-13 地址:

https://www.jianshu.com/p/e83fe2a57bcd



以上资料内容来源网络,仅供学习交流,侵删请私信我,谢谢。

你可能感兴趣的:(第五章 爬虫进阶(二十六) 2020-02-12)