美文网首页
大众点评模拟登陆

大众点评模拟登陆

作者: 领悟悟悟 | 来源:发表于2017-12-20 16:51 被阅读0次
    def dazhongdianping_login(phone):
       '''
      大众点评模拟登陆,随机睡眠
      :param phone: 手机号
      :return: 登陆的cookie
      '''
      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      import random
    
      driver = webdriver.Chrome()
      driver.get('http://www.dianping.com/')
      # assert "Python" in driver.title
      # elem = driver.find_element_by_id('kw')
      # elem.send_keys("大众点评")
      # elem.send_keys(Keys.RETURN)
      # 自动点击登陆
      sleep(random.uniform(1, 3))
      elem = driver.find_element_by_xpath(r'//*[@id="top-nav"]/div/div[2]/span[2]/a[1]')
      elem.click()
      # 切入网页框架
      sleep(random.uniform(1, 3))
      driver.switch_to.frame(driver.find_element_by_xpath(r'//*[@id="J_login_container"]/div/iframe'))  # 切入
      # 点击账号登录
      driver.find_element_by_xpath(r"/html/body/div/div[2]/div[5]/span").click()
    
      # print(driver.page_source)
      # 输入验证码
      sleep(random.uniform(1, 3))
      driver.find_element_by_xpath(r'//*[@id="mobile-number-textbox"]').send_keys(phone[:3])
      sleep(random.uniform(0,2))
      driver.find_element_by_xpath(r'//*[@id="mobile-number-textbox"]').send_keys(phone[3:7])
      sleep(random.uniform(0,2))
      driver.find_element_by_xpath(r'//*[@id="mobile-number-textbox"]').send_keys(phone[7:])
    
      # 点击获取验证码,等待输入
      sleep(random.uniform(1, 3))
      driver.find_element_by_xpath(r'//*[@id="send-number-button"]').click()
      key = input('请输入验证码:')
      driver.find_element_by_xpath(r'//*[@id="number-textbox"]').send_keys(key)
      # 点击登陆
      sleep(random.uniform(0,1))
      driver.find_element_by_xpath(r'//*[@id="login-button-mobile"]').click()
      driver.switch_to.default_content() # 切出框架
      sleep(random.uniform(2,3))
      # 处理cookie
      cookie = driver.get_cookies()
      result = {}
      for each in cookie:
        result[each['name']] = each['value']
      return result

    相关文章

      网友评论

          本文标题:大众点评模拟登陆

          本文链接:https://www.haomeiwen.com/subject/fgmgwxtx.html