之前用了谷歌的selenium模块,为了练习一下 所以写了这个(没错 才不是别的理由!)
代码里涉及到了各种复杂的情况,包括要找的div在别的iframe中的处理方式,顶部导航栏的定位方式等等。综合性很强
下面是代码
from selenium import webdriver
import time
from fake_useragent import UserAgent
opt = webdriver.ChromeOptions()
# opt.add_argument('--headless')
# 更换头部
ua = UserAgent()
opt.add_argument('user-agent="%s"' % ua.random)
driver = webdriver.Chrome(chrome_options=opt)
class HongZha:
def __init__(self):
self.phone = 'xxxxxxxxxxx' # your phone number
self.num = 0
# 发送验证码
def send_yzm(self, button, name):
button.click()
self.num += 1
print("{} 第{}次 发送成功 {}".format(self.phone, self.num, name))
def youku(self, name):
driver.get('https://www.youku.com/')
driver.implicitly_wait(10)
driver.find_element_by_xpath('//img[@class="a-avatar_17DKW"]').click()
time.sleep(3)
driver.find_element_by_xpath('//a[@href="javascript:void(0);"]').click()
time.sleep(3)
driver.find_element_by_xpath('//input[@class="fm-text"]').send_keys(self.phone)
button = driver.find_element_by_xpath('//a[@class="send-btn-link"]')
self.send_yzm(button, name)
# 瓜子注册接口
def guazi(self, name):
try:
driver.implicitly_wait(10)
driver.get("https://www.guazi.com/www/bj/buy")
a_btn = driver.find_element_by_xpath("//a[@class='uc-my']")
a_btn.click()
tel = driver.find_element_by_xpath("//input[@name='phone']")
tel.send_keys(self.phone)
button = driver.find_element_by_xpath("//button[@class='get-code']")
self.send_yzm(button, name)
except:
print('faled')
# 唯品会注册接口
def wphui(self, name):
driver.get("https://passport.vip.com/register?src=https%3A%2F%2Fwww.vip.com%2F")
driver.implicitly_wait(10)
tel = driver.find_element_by_xpath("//input[@placeholder='请输入手机号码']")
tel.send_keys(self.phone)
driver.find_element_by_xpath('//a[contains(./text(),"获取验证码")]').click()
button = driver.find_element_by_xpath("//a[@class='ui-btn-medium btn-verify-code ui-btn-secondary']")
self.send_yzm(button, name)
# 有赞注册接口
def youzan(self, name):
driver.get('https://account.youzan.com/login')
driver.implicitly_wait(10)
driver.find_element_by_xpath('//img[@src="//b.yzcdn.cn/wsc-pc-account/images/[email protected]"]').click()
driver.find_element_by_xpath('//li[@class="js-tab-captcha-login"]').click()
driver.find_elements_by_xpath('//input[@name="phoneInfo"]')[0].send_keys(self.phone)
button = driver.find_elements_by_xpath('//div[@class="sms-btn js-get-identify-code"]')[0]
self.send_yzm(button, name)
# 拼多多短信登陆接口
def pinduoduo(self, name):
driver.get('http://mobile.yangkeduo.com/login.html')
driver.implicitly_wait(10)
driver.find_element_by_xpath('//div[@class="phone-login"]/span').click()
driver.find_element_by_xpath('//input[@id="user-mobile"]').send_keys(self.phone)
time.sleep(3)
button=driver.find_element_by_xpath('//button[@id="code-button"]')
self.send_yzm(button, name)
def wangyiyun(self, name):
driver.get('https://music.163.com/')
driver.implicitly_wait(10)
driver.find_elements_by_xpath('//a[@class="link s-fc3"]')[0].click()
driver.find_element_by_xpath('//a[@class="u-btn2 u-btn2-1"]').click()
driver.find_element_by_xpath('//input[@class="j-phone txt u-txt"]').send_keys(self.phone)
driver.find_element_by_xpath('//input[@class="j-pwd u-txt"]').send_keys('woshinibaba123')
button = driver.find_element_by_xpath('//a[@class="j-btn u-btn2 u-btn2-2"]')
self.send_yzm(button, name)
def weibo(self, name):
try:
driver.get('https://weibo.com/signup/signup.php')
driver.implicitly_wait(10)
driver.find_elements_by_xpath('//input[@class="tel_num"]')[0].send_keys(self.phone)
driver.find_element_by_xpath('//input[@class="W_input"]').send_keys('woshinibibi123')
s = driver.find_element_by_xpath('//select[@class="sel year"]')
s.find_element_by_xpath('//option[@value="1996"]').click()
s = driver.find_element_by_xpath('//select[@class="sel month"]')
s.find_element_by_xpath('//option[@value="1"]').click()
s = driver.find_element_by_xpath('//select[@class="sel day"]')
s.find_element_by_xpath('//option[@value="1"]').click()
button = driver.find_element_by_xpath('//a[@class="W_btn_e"]')
self.send_yzm(button, name)
except:
pass
def baiduyun(self, name):
driver.get('https://login.bce.baidu.com/reg.html?tpl=bceplat&from=portal')
driver.implicitly_wait(10)
driver.find_elements_by_xpath('//input[@placeholder="用户名"]')[0].send_keys('123sasdsa')
driver.find_elements_by_xpath('//input[@placeholder="密码"]')[0].send_keys('Aa123123123')
driver.find_elements_by_xpath('//input[@placeholder="确认密码"]')[0].send_keys('Aa123123123')
driver.find_elements_by_xpath('//input[@placeholder="手机号"]')[0].send_keys(self.phone)
button = driver.find_element_by_xpath('//button[@class="btn-light"]')
self.send_yzm(button, name)
def huaweiyun(self, name):
driver.get('https://reg.huaweicloud.com/registerui/cn/register.html?locale=zh-cn&channelFrom=MTAyMQ==&fromuser=null&service=https%3A%2F%2Factivity.huaweicloud.com%2F2019august_promotion%2Findex.html%3Futm_source%3Dbaidu-a%26utm_medium%3Dse-cpc-op%26utm_campaign%3DPP-%25E7%25AB%259E%25E5%2593%2581%26utm_content%3DPP-%25E7%25AB%259E%25E5%2593%2581-%25E9%2598%25BF%25E9%2587%258C%25E4%25BA%2591%26utm_term%3D%25E9%2598%25BF%25E9%2587%258C%25E4%25BA%2591#/register')
driver.implicitly_wait(10)
driver.find_elements_by_xpath('//input[@placeholder="请输入您的手机号"]')[0].send_keys(self.phone)
button = driver.find_elements_by_xpath('//div[@id="msgBtn"]')[0]
self.send_yzm(button, name)
def yinxiang(self, name):
driver.get('https://static.app.yinxiang.com/embedded-web/registration/index.html?targetUrl=%2FHome.action#/registration')
driver.implicitly_wait(10)
driver.find_elements_by_xpath('//input[@class="registration-account-input "]')[0].send_keys(self.phone)
driver.find_elements_by_xpath('//input[@placeholder="设置密码,至少6位字符"]')[0].send_keys('woshinibaba123123')
button = driver.find_elements_by_xpath('//div[@class="registration-sms-vercode-btn-validate"]')[0]
self.send_yzm(button, name)
time.sleep(3)
def douban(self, name):
driver.get('https://www.douban.com/')
driver.implicitly_wait(10)
iframe = driver.find_elements_by_tag_name("iframe")[0] # 由于要找的input在iframe中,直接定位不到该目标,要先转换到其所在的iframe中才行
driver.switch_to_frame(iframe)
driver.find_elements_by_xpath('//input[@name="phone"]')[0].send_keys(self.phone)
button = driver.find_elements_by_xpath('//div[@class="account-form-field-code"]')[0]
self.send_yzm(button, name)
time.sleep(3)
def aibiying(self, name):
driver.get('https://www.airbnb.cn/?af=43896654&c=.pi9.pkbaidu_brd_brandzone_demand_title_p1&src=Baidu&medium=PPC&ag_kwid=2299-36-57701246c0b98773.6a0cc0f87b49337e')
driver.implicitly_wait(10)
driver.find_elements_by_xpath('//div[@class="_18lcoy3z"]')[7].click() # 顶部导航栏直接定位不到,要先定位导航栏,再逐步定位
driver.find_elements_by_xpath('//input[@class="_kbzo2td"]')[0].send_keys(self.phone)
button = driver.find_elements_by_xpath('//button[@class="_1wficfyg"]')[0]
self.send_yzm(button, name)
time.sleep(3)
# 循环执行
def main(self):
while True:
self.guazi('瓜子')
time.sleep(3)
self.pinduoduo('拼多多')
time.sleep(3)
self.wphui('唯品会')
time.sleep(3)
self.youzan('有赞')
time.sleep(3)
self.wangyiyun('网易云')
time.sleep(3)
self.weibo('微博')
time.sleep(3)
self.baiduyun('百度云')
time.sleep(3)
self.huaweiyun('华为云')
time.sleep(3)
self.yinxiang('印象笔记')
time.sleep(3)
self.douban('豆瓣')
time.sleep(3)
self.aibiying('爱彼迎')
time.sleep(10800)
if __name__ == '__main__':
hongzha = HongZha()
hongzha.main()