1. 程式人生 > >利用Python實現批量註冊網站用戶,註意不可用於商業用途哦!

利用Python實現批量註冊網站用戶,註意不可用於商業用途哦!

ora asi text 用途 parent 51cto containe orm aci

場景目標

現在大多數網站的「用戶註冊功能」都需要用戶正確輸入了驗證碼,才能發起註冊的請求,如果想大量註冊用戶,正確識別驗證碼變的很關鍵。

普通的驗證碼使用 tesserocr,加上訓練可以完成,如果想簡單一點,可以使用「百度雲的文字識別 API」。

今天的目標是使用 selenium + 百度雲OCR 批量註冊「中知網」一批用戶。
技術分享圖片
技術分享圖片
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from aip import AipOcr

""" 你的 APPID AK SK """

APP_ID = ‘1547‘

API_KEY = ‘VBoMZ6XUX11‘

SECRET_KEY = ‘GPvqLVeGIMOR57**‘

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

</pre>

分析思路

我們打開中國知網的註冊頁面,檢查驗證碼圖片的元素,通過 src 屬性可以知道驗證碼的請求地址是:

「http://my.cnki.net/elibregister/CheckCode.aspx」

每次刷新頁面或者點擊驗證碼圖片,都會重新加載一次驗證碼。這裏我們只能使用截取驗證碼區域保存到本地。

技術分享圖片
另外,截圖驗證碼圖片,需要使用「Phtotoshop」來獲取驗證碼的左上角和右下角的坐標數據。
技術分享圖片
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def get_code(self):

1.截圖並保存到本地

self.driver.get_screenshot_as_file(‘./%s‘ % self.screen_shot_file_name)

2.打開文件

screenshot_image = Image.open(‘./%s‘ % self.screen_shot_file_name)

3.設置要裁剪的區域(驗證碼所在的區域)

code_box = (899, 819, 1048, 883)

4.截圖:生成只有驗證碼的圖片

code_image = screenshot_image.crop(code_box)

5.保存到本地

code_image.save("./%s" % self.code_file_name)

6.以byte讀取圖片

image = get_file_content("./%s" % self.code_file_name)

7.使用百度OCR識別驗證碼

result = client.basicAccurate(image)

print(result)

識別的文字內容

word_result = result.get(‘words_result‘)[0].get(‘words‘)

return word_result

</pre>

然後使用 Image 類的 crop() 函數截取驗證碼圖片並保存到本地,接著可以調用百度雲 OCR API 去識別驗證碼。

另外,由於驗證碼識別率有一定幾率的失敗,需要循環去填入驗證碼,然後點擊外側容器元素操作,直到驗證碼識別正確。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">while True:

code = self.get_code().strip()

error_tips_element = self.driver.find_element_by_id(‘span_oldcheckcode‘)

print(‘驗證碼為:%s‘ % code)

code_input_element.clear()

code_input_element.click()

code_input_element.send_keys(code)

點擊外圍的容器,判斷驗證碼是否輸入正確

container_element.click()

顯示了錯誤信息:驗證碼輸入錯誤

if error_tips_element.text:

time.sleep(2)

print(‘驗證碼驗證失敗,點擊驗證碼圖片‘)

點擊驗證碼圖片,重新加載驗證碼

code_img_element.click()

continue

else:

print(‘驗證碼驗證成功‘)

break

</pre>

現在就可以通過 webdriver 獲取到其他輸入框元素,填充用戶名、密碼、郵箱地址,點擊註冊按鈕,實現註冊一個用戶的功能。
技術分享圖片
多次循環上面的操作,就可以實現批量註冊的需求。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def register(self, code):

用戶名輸入框

username_input_element = self.driver.find_element_by_id(‘username‘)

密碼輸入框

password_input_element = self.driver.find_element_by_id(‘txtPassword‘)

郵箱輸入框

txtEmail_input_element = self.driver.find_element_by_id(‘txtEmail‘)

註冊按鈕

submit_btn_element = self.driver.find_element_by_id(‘ButtonRegister‘)

username_input_element.send_keys(self.username)

password_input_element.send_keys(self.password)

txtEmail_input_element.send_keys(self.email)

submit_btn_element.click()

</pre>

註:不能用於商業用途,批量註冊然後在販賣號哦!

最後,如果你跟我一樣都喜歡python,想成為一名優秀的程序員,也在學習python的道路上奔跑,歡迎你加入python學習群:839383765 群內每天都會分享最新業內資料,分享python免費課程,共同交流學習,讓學習變(編)成(程)一種習慣!

利用Python實現批量註冊網站用戶,註意不可用於商業用途哦!