citys4.py
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from lxml import etree
from day05.sms_helper import get_mobile, get_sms_message
from day05.captcha_helper import *
# 设置浏览器参数
chrome_options = webdriver.ChromeOptions()
# 设置成无头浏览器
# chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(browser, 5)
def get_page():
browser.get('https://passport.4c.cn/signup')
# 获取手机号
mobile = get_mobile()
print(mobile)
# 获取手机号输入框
mobile_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mobile')))
mobile_input.clear()
mobile_input.send_keys(mobile)
time.sleep(1)
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#show-captcha')))
button.click()
time.sleep(1)
# 获取图形验证码
captcha = get_captcha(browser, '.captcha-img')
print(captcha)
# 输入图形验证码
captcha_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#captcha')))
captcha_input.clear()
captcha_input.send_keys(captcha)
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#sendsms')))
button.click()
time.sleep(1)
# 获取短信验证码
sms_code = get_sms_message(mobile)
print(sms_code)
sms_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#code')))
sms_input.clear()
sms_input.send_keys(sms_code)
# 输入密码
pwd_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#password')))
pwd_input.clear()
pwd_input.send_keys('DEER1234567')
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#nextstep')))
button.click()
def main():
get_page()
if __name__ == '__main__':
main()
captcha_helper.py
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from PIL import Image
from io import BytesIO
from .chaojiying import main1
# 取浏览器窗口内全图
def get_big_image(browser):
# browser.execute_script('window.scrollTo(0, 300)')
screenshot = browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot
def get_captha_position(browser, class_str):
wait = WebDriverWait(browser, 5)
captha = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, class_str)))
location = captha.location
size = captha.size
x1 = location['x']
y1 = location['y']
width = size['width']
height = size['height']
x2 = x1 + width
y2 = y1 + height
print(x1, y1, x2, y2)
print(width, height)
return (x1, y1, x2, y2)
def get_captcha(browser, class_str):
full_screen_img = get_big_image(browser)
full_screen_img.save('mobile_login.png')
# 获取验证码左上角和右下角坐标
x1, y1, x2, y2 = get_captha_position(browser, class_str)
captha_img = full_screen_img.crop((x1, y1, x2, y2))
captha_img.save('mobile_captha.png')
captha_str = main1('mobile_captha.png')
return captha_str
sms_helper.py
# -*- coding: utf-8 -*-
# python3.5
# 易码短信服务平台开放接口范例代码
# 语言版本:python版
# 官方网址:www.51ym.me
# 技术支持QQ:2114927217
# 发布时间:217-12-11
from urllib import parse, request
import time
import re
header_dict = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
# 登陆/获取TOKEN
username = 'carmack' # 账号
password = 'Vff123456' # 密码
url = 'http://api.fxhyd.cn/UserInterface.aspx?action=login&username=' + \
username+'&password='+password
TOKEN1 = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
if TOKEN1.split('|')[0] == 'success':
TOKEN = TOKEN1.split('|')[1]
print('TOKEN是'+TOKEN)
else:
print('获取TOKEN错误,错误代码'+TOKEN1+'。代码释义:1001:参数token不能为空;1002:参数action不能为空;1003:参数action错误;1004:token失效;1005:用户名或密码错误;1006:用户名不能为空;1007:密码不能为空;1008:账户余额不足;1009:账户被禁用;1010:参数错误;1011:账户待审核;1012:登录数达到上限')
TOKEN = '01138099c52f07a264e5d5b27d17fb04e6b9a8dfb501' # 输入TOKEN
# 获取账户信息
url = 'http://api.fxhyd.cn/UserInterface.aspx?action=getaccountinfo&token='+TOKEN+'&format=1'
ACCOUNT1 = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
if ACCOUNT1.split('|')[0] == 'success':
ACCOUNT = ACCOUNT1.split('|')[1]
print(ACCOUNT)
else:
print('获取TOKEN错误,错误代码'+ACCOUNT1)
ITEMID = '5242' # 项目编号
EXCLUDENO = '' # 排除号段170_171
# # 获取手机号码
# ITEMID = '694' # 项目编号
# EXCLUDENO = '' # 排除号段170_171
# url = 'http://api.fxhyd.cn/UserInterface.aspx?action=getmobile&token=' + \
# TOKEN+'&itemid='+ITEMID+'&excludeno='+EXCLUDENO
# MOBILE1 = request.urlopen(request.Request(
# url=url, headers=header_dict)).read().decode(encoding='utf-8')
# if MOBILE1.split('|')[0] == 'success':
# MOBILE = MOBILE1.split('|')[1]
# print('获取号码是:\n'+MOBILE)
# else:
# print('获取TOKEN错误,错误代码'+MOBILE1)
def get_mobile():
# 获取手机号码
url = 'http://api.fxhyd.cn/UserInterface.aspx?action=getmobile&token=' + \
TOKEN+'&itemid='+ITEMID+'&excludeno='+EXCLUDENO
MOBILE1 = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
if MOBILE1.split('|')[0] == 'success':
MOBILE = MOBILE1.split('|')[1]
print('获取号码是:\n'+MOBILE)
return MOBILE
else:
print('获取TOKEN错误,错误代码'+MOBILE1)
def release_mobile(MOBILE):
# 释放号码
url = 'http://api.fxhyd.cn/UserInterface.aspx?action=release&token=' + \
TOKEN+'&itemid='+ITEMID+'&mobile='+MOBILE
RELEASE = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
if RELEASE == 'success':
print('号码成功释放')
def get_sms_message(MOBILE):
# 获取短信,注意线程挂起5秒钟,每次取短信最少间隔5秒
WAIT = 100 # 接受短信时长60s
url = 'http://api.fxhyd.cn/UserInterface.aspx?action=getsms&token=' + \
TOKEN+'&itemid='+ITEMID+'&mobile='+MOBILE+'&release=1'
text1 = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
TIME1 = time.time()
TIME2 = time.time()
ROUND = 1
while (TIME2-TIME1) < WAIT and not text1.split('|')[0] == "success":
time.sleep(5)
text1 = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
TIME2 = time.time()
ROUND = ROUND+1
ROUND = str(ROUND)
if text1.split('|')[0] == "success":
text = text1.split('|')[1]
TIME = str(round(TIME2-TIME1, 1))
print('短信内容是'+text+'\n耗费时长'+TIME+'s,循环数是'+ROUND)
# 提取短信内容中的数字验证码
pat = "验证码(.*?),"
IC = 0
IC = re.findall(pat, text)
if IC:
print("验证码是:\n"+IC[0])
return IC[0]
else:
print('获取短信超时,错误代码是'+text1+',循环数是'+ROUND)
'''
# 释放号码
url = 'http://api.fxhyd.cn/UserInterface.aspx?action=release&token=' + \
TOKEN+'&itemid='+ITEMID+'&mobile='+MOBILE
RELEASE = request.urlopen(request.Request(
url=url, headers=header_dict)).read().decode(encoding='utf-8')
if RELEASE == 'success':
print('号码成功释放')
# 提取短信内容中的数字验证码
pat = "[0-9]+"
IC = 0
IC = re.search(pat, text)
if IC:
print("验证码是:\n"+IC.group())
else:
print("请重新设置表达式")
'''
def main():
# mobile = get_mobile()
# print(mobile)
message = get_sms_message('13766128934')
print(message)
# time.sleep(1)
# release_mobile('15130550540')
if __name__ == '__main__':
main()