最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Selenium之模擬登錄鐵路12306的示例代碼

 更新時(shí)間:2020年07月31日 11:29:25   作者:赤道上的一滴雪  
這篇文章主要介紹了Selenium之模擬登錄鐵路12306的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

最近接觸了一些selenium模塊的相關(guān)知識(shí),覺(jué)得還挺有意思的,于是決定親自嘗試寫一些爬蟲程序來(lái)強(qiáng)化selenium模塊(一定要多嘗試、多動(dòng)手、多總結(jié))。本文主要使用python爬蟲來(lái)模擬登錄鐵路12306官網(wǎng)。這兒得吐槽一句,鐵路12306網(wǎng)站的反爬機(jī)制做的還是比較好。

話不多說(shuō),下面跟小墨一起來(lái)學(xué)習(xí)如何通過(guò)爬蟲來(lái)實(shí)現(xiàn)鐵路12306的登錄。

一、 驗(yàn)證碼破解

當(dāng)我們輸入賬號(hào)和密碼后,在點(diǎn)擊登錄按鈕之前,還需要對(duì)驗(yàn)證碼進(jìn)行操作。對(duì)驗(yàn)證碼的識(shí)別,已經(jīng)有相關(guān)的處理平臺(tái),我們只需要借助第三方平臺(tái)即可。

1.注冊(cè)并登錄超級(jí)鷹賬號(hào):點(diǎn)擊鏈接進(jìn)行注冊(cè)https://www.chaojiying.com/user/login/;
2.點(diǎn)擊購(gòu)買題分,并進(jìn)行充值;
3.點(diǎn)擊軟件id,創(chuàng)建一個(gè)軟件Id(程序中會(huì)用到);
4.下載示例代碼(開(kāi)發(fā)文檔—>選擇相應(yīng)的語(yǔ)言–>下載示例demo),python示例代碼如下所示:

class Chaojiying_Client(object):
 def __init__(self, username, password, soft_id):
 self.username = username
 password = password.encode('utf8')
 self.password = md5(password).hexdigest()
 self.soft_id = soft_id
 self.base_params = {
  'user': self.username,
  'pass2': self.password,
  'softid': self.soft_id,
 }
 self.headers = {
  'Connection': 'Keep-Alive',
  'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
 }
 def PostPic(self, im, codetype):
 """
 im: 圖片字節(jié)
 codetype: 題目類型 參考 http://www.chaojiying.com/price.html
 """
 params = {
  'codetype': codetype,
 }
 params.update(self.base_params)
 files = {'userfile': ('ccc.jpg', im)}
 r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
 return r.json()
 def ReportError(self, im_id):
 """
 im_id:報(bào)錯(cuò)題目的圖片ID
 """
 params = {
  'id': im_id,
 }
 params.update(self.base_params)
 r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
 return r.json()

二、Selenium功能簡(jiǎn)介

Selenium模塊和爬蟲之間的關(guān)聯(lián):
–便捷的獲取網(wǎng)站中的動(dòng)態(tài)加載數(shù)據(jù)
–便捷實(shí)現(xiàn)模擬登錄

Selenium模塊的使用流程:
–環(huán)境安裝:pip install selenium
–下載瀏覽器的驅(qū)動(dòng)程序(谷歌瀏覽器):
–下載路徑:http://chromedriver.storage.googleapis.com/index.html
– 驅(qū)動(dòng)程序和瀏覽器的映射關(guān)系:映射鏈接
–將下載好的驅(qū)動(dòng)程序放在當(dāng)前項(xiàng)目目錄下

Selenium模塊的相關(guān)方法:http://m.fzitv.net/article/192259.htm

上述內(nèi)容完成后,我們就可以正式進(jìn)入正題了,是不是很期待,那就跟著小墨往下走吧。

三、模擬登錄

1. 進(jìn)入官網(wǎng)

#創(chuàng)建對(duì)象
#executable_path=path:下載好的驅(qū)動(dòng)程序的路徑
bro = webdriver.Chrome(executable_path='chromedriver.exe')
#12306的登錄網(wǎng)址
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
#窗口最大化
bro.maximize_window()

2、進(jìn)入登錄界面并獲取驗(yàn)證碼

#save_screenshot就是將當(dāng)前頁(yè)面進(jìn)行截圖且保存
bro.save_screenshot('aa.png')
#確定驗(yàn)證碼圖片對(duì)應(yīng)的左上角和右下角的坐標(biāo)(裁剪的區(qū)域就確定)
code_img_ele = bro.find_element_by_xpath('//*[@id="J-loginImg"]')
location = code_img_ele.location # 驗(yàn)證碼圖片左上角的坐標(biāo) x,y
#print('location:',location)
size = code_img_ele.size #驗(yàn)證碼標(biāo)簽對(duì)應(yīng)的長(zhǎng)和寬
#print('size:',size)
#左上角和右下角坐標(biāo)
rangle = (
int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
#至此驗(yàn)證碼圖片區(qū)域就確定下來(lái)了
i = Image.open('./aa.png')
code_img_name = './code.png'
#crop根據(jù)指定區(qū)域進(jìn)行圖片裁剪
frame = i.crop(rangle)
frame.save(code_img_name)
#將驗(yàn)證碼圖片提交給超級(jí)鷹進(jìn)行識(shí)別
chaojiying = Chaojiying_Client('########', '#######', '#######')	#用戶賬號(hào)>>密碼>>軟件ID 
im = open('code.png', 'rb').read()									#本地圖片文件路徑 來(lái)替換 a.jpg 有時(shí)WIN系統(tǒng)須要//
id=chaojiying.PostPic(im, 9004)['pic_id']    #截取的驗(yàn)證碼照片以及驗(yàn)證碼的類別代號(hào)
result = chaojiying.PostPic(im, 9004)['pic_str']   #識(shí)別結(jié)果
all_list = [] #要存儲(chǔ)即將被點(diǎn)擊的點(diǎn)的坐標(biāo) [[x1,y1],[x2,y2]]
#識(shí)別錯(cuò)誤后,會(huì)返回題分,示例代碼并沒(méi)有這個(gè),就是想讓你花錢
chaojiying.ReportError(id)
if '|' in result:
 list_1 = result.split('|')
 print(list_1)
 count_1 = len(list_1)
 for i in range(count_1):
 xy_list = []
 x = int(list_1[i].split(',')[0])
 y = int(list_1[i].split(',')[1])
 xy_list.append(x)
 xy_list.append(y)
 all_list.append(xy_list)
else:
 x = int(result.split(',')[0])
 y = int(result.split(',')[1])
 xy_list = []
 xy_list.append(x)
 xy_list.append(y)
 all_list.append(xy_list)
#遍歷列表,使用動(dòng)作鏈對(duì)每一個(gè)列表元素對(duì)應(yīng)的x,y指定的位置進(jìn)行點(diǎn)擊操作
for l in all_list:
 x = l[0]
 y = l[1]
 ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
 time.sleep(0.5)

這樣我們就實(shí)現(xiàn)了驗(yàn)證碼的識(shí)別操作。

3、輸入賬號(hào)和密碼,并點(diǎn)擊登錄按鈕

#輸入賬號(hào)和密碼
 put1=bro.find_element_by_id('J-userName')
 #當(dāng)驗(yàn)證碼識(shí)別錯(cuò)誤后,需要清空賬號(hào)重新輸入
 put1.clear()
 #輸入賬號(hào)
 put1.send_keys('########')
 time.sleep(1)
 put2=bro.find_element_by_id('J-password')
 put2.clear()
 #輸入密碼
 put2.send_keys('##########')
 time.sleep(1)
 #點(diǎn)擊登錄按鈕
 bro.find_element_by_id('J-login').click()

點(diǎn)擊登錄按鈕后,會(huì)出現(xiàn)如下圖所示的彈框

因此,我們需要定位到該提示框,并實(shí)現(xiàn)滑塊的向右滑動(dòng)

4、滑塊滑動(dòng)

#處理提示框
time.sleep(0.5)
span=bro.find_element_by_xpath('//*[@id="nc_1_n1z"]')
action = ActionChains(bro)
#點(diǎn)擊長(zhǎng)按指定的標(biāo)簽
action.click_and_hold(span).perform()
action.drag_and_drop_by_offset(span,400,0).perform()

有的時(shí)候,當(dāng)滑塊移動(dòng)后,會(huì)出現(xiàn)如下圖所示的情況:


因此,我們需要點(diǎn)擊刷新,并重新進(jìn)行滑塊的移動(dòng),所以對(duì)代碼做稍微的改動(dòng):

while True:
 try:
  info=bro.find_element_by_xpath('//*[@id="J-slide-passcode"]/div/span').text
  print(info)
  if info=='哎呀,出錯(cuò)了,點(diǎn)擊刷新再來(lái)一次':
  	 #點(diǎn)擊刷新
  bro.find_element_by_xpath('//*[@id="J-slide-passcode"]/div/span/a').click()
  time.sleep(0.2)
  #重新移動(dòng)滑塊
  span = bro.find_element_by_xpath('//*[@id="nc_1_n1z"]')
  action = ActionChains(bro)
  # 點(diǎn)擊長(zhǎng)按指定的標(biāo)簽
  action.click_and_hold(span).perform()
  action.drag_and_drop_by_offset(span, 400, 0).perform()
  time.sleep(7)
 except:
  print('ok!')
  break

至此,我們便實(shí)現(xiàn)了鐵路12306的登錄,如下圖所示


是不是覺(jué)得很簡(jiǎn)單啊。

5、完整代碼

# -*- coding: utf-8 -*-

#驗(yàn)證碼識(shí)別示例
import requests
from hashlib import md5
class Chaojiying_Client(object):
 def __init__(self, username, password, soft_id):
 self.username = username
 password = password.encode('utf8')
 self.password = md5(password).hexdigest()
 self.soft_id = soft_id
 self.base_params = {
  'user': self.username,
  'pass2': self.password,
  'softid': self.soft_id,
 }
 self.headers = {
  'Connection': 'Keep-Alive',
  'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
 }
 def PostPic(self, im, codetype):
 """
 im: 圖片字節(jié)
 codetype: 題目類型 參考 http://www.chaojiying.com/price.html
 """
 params = {
  'codetype': codetype,
 }
 params.update(self.base_params)
 files = {'userfile': ('ccc.jpg', im)}
 r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
 return r.json()
 def ReportError(self, im_id):
 """
 im_id:報(bào)錯(cuò)題目的圖片ID
 """
 params = {
  'id': im_id,
 }
 params.update(self.base_params)
 r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
 return r.json()

#使用selenium打開(kāi)登錄頁(yè)面
from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC, wait

#創(chuàng)建對(duì)象
#executable_path=path:下載好的驅(qū)動(dòng)程序的路徑
bro = webdriver.Chrome(executable_path='chromedriver.exe')
#12306的登錄網(wǎng)址
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
#窗口最大化
bro.maximize_window()
#點(diǎn)擊賬號(hào)登錄
bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
time.sleep(1)
while True:
 try:
 #save_screenshot就是將當(dāng)前頁(yè)面進(jìn)行截圖且保存
 bro.save_screenshot('aa.png')
 #確定驗(yàn)證碼圖片對(duì)應(yīng)的左上角和右下角的坐標(biāo)(裁剪的區(qū)域就確定)
 code_img_ele = bro.find_element_by_xpath('//*[@id="J-loginImg"]')
 location = code_img_ele.location # 驗(yàn)證碼圖片左上角的坐標(biāo) x,y
 #print('location:',location)
 size = code_img_ele.size #驗(yàn)證碼標(biāo)簽對(duì)應(yīng)的長(zhǎng)和寬
 #print('size:',size)
 #左上角和右下角坐標(biāo)
 rangle = (
 int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
 #至此驗(yàn)證碼圖片區(qū)域就確定下來(lái)了
 i = Image.open('./aa.png')
 code_img_name = './code.png'
 #crop根據(jù)指定區(qū)域進(jìn)行圖片裁剪
 frame = i.crop(rangle)
 frame.save(code_img_name)
 #將驗(yàn)證碼圖片提交給超級(jí)鷹進(jìn)行識(shí)別
 chaojiying = Chaojiying_Client('#####', '#######', '######')	#用戶賬號(hào)>>密碼>>軟件ID
 im = open('code.png', 'rb').read()									#本地圖片文件路徑 來(lái)替換 a.jpg 有時(shí)WIN系統(tǒng)須要//
 id=chaojiying.PostPic(im, 9004)['pic_id']    #截取的驗(yàn)證碼照片以及驗(yàn)證碼的類別代號(hào)
 result = chaojiying.PostPic(im, 9004)['pic_str']   #識(shí)別結(jié)果
 all_list = [] #要存儲(chǔ)即將被點(diǎn)擊的點(diǎn)的坐標(biāo) [[x1,y1],[x2,y2]]
 #識(shí)別錯(cuò)誤后,會(huì)返回題分,官網(wǎng)給的demo并沒(méi)有這一句,哈哈哈,坑吧,就是讓你多花錢
 chaojiying.ReportError(id)
 if '|' in result:
  list_1 = result.split('|')
  print(list_1)
  count_1 = len(list_1)
  for i in range(count_1):
  xy_list = []
  x = int(list_1[i].split(',')[0])
  y = int(list_1[i].split(',')[1])
  xy_list.append(x)
  xy_list.append(y)
  all_list.append(xy_list)
 else:
  x = int(result.split(',')[0])
  y = int(result.split(',')[1])
  xy_list = []
  xy_list.append(x)
  xy_list.append(y)
  all_list.append(xy_list)
 #遍歷列表,使用動(dòng)作鏈對(duì)每一個(gè)列表元素對(duì)應(yīng)的x,y指定的位置進(jìn)行點(diǎn)擊操作
 for l in all_list:
  x = l[0]
  y = l[1]
  ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
  time.sleep(0.5)
 #輸入賬號(hào)和密碼
 put1=bro.find_element_by_id('J-userName')
 #當(dāng)驗(yàn)證碼識(shí)別錯(cuò)誤后,需要清空賬號(hào)重新輸入
 put1.clear()
 put1.send_keys('username') #你的賬號(hào)
 time.sleep(1)
 put2=bro.find_element_by_id('J-password')
 put2.clear()
 put2.send_keys('password') #你的密碼
 time.sleep(1)
 bro.find_element_by_id('J-login').click()
 #處理提示框
 time.sleep(3)
 span=bro.find_element_by_xpath('//*[@id="nc_1_n1z"]')
 action = ActionChains(bro)
 #點(diǎn)擊長(zhǎng)按指定的標(biāo)簽
 action.click_and_hold(span).perform()
 action.drag_and_drop_by_offset(span,400,0).perform()
 time.sleep(8)
 while True:
  try:
  info=bro.find_element_by_xpath('//*[@id="J-slide-passcode"]/div/span').text
  print(info)
  if info=='哎呀,出錯(cuò)了,點(diǎn)擊刷新再來(lái)一次':
   bro.find_element_by_xpath('//*[@id="J-slide-passcode"]/div/span/a').click()
   time.sleep(0.2)
   span = bro.find_element_by_xpath('//*[@id="nc_1_n1z"]')
   action = ActionChains(bro)
   # 點(diǎn)擊長(zhǎng)按指定的標(biāo)簽
   action.click_and_hold(span).perform()
   action.drag_and_drop_by_offset(span, 400, 0).perform()
   time.sleep(7)
  except:
  print('ok!')
  break
 #釋放動(dòng)作鏈
 action.release()
 break
 except:
 time.sleep(3)
time.sleep(12)
#登錄成功
bro.find_element_by_link_text('確定').click()
time.sleep(0.5)
bro.find_element_by_link_text('首頁(yè)').click()
#輸入起點(diǎn)、終點(diǎn)以及時(shí)間,查詢車票
start_city='北京'
end_city='上海'
date='2020-08-05'
#選擇起點(diǎn)
bro.find_element_by_xpath('//*[@id="fromStationText"]').click()
time.sleep(2)
#這只遍歷了熱門城市,要是想遍歷其他城市,自己寫一個(gè)循環(huán)就行
city_list=bro.find_elements_by_xpath('//*[@id="ul_list1"]/li')
for city in city_list:
 if city.text==start_city:
 city.click()
 break
time.sleep(2)
#選擇終點(diǎn)
bro.find_element_by_xpath('//*[@id="toStationText"]').click()
for city in city_list:
 if city.text==end_city:
 city.click()
 break
time.sleep(2)
js = "$('input[id=train_date]').removeAttr('readonly')"
bro.execute_script(js)
dt=bro.find_element_by_id('train_date')
dt.clear()
dt.send_keys(date)
time.sleep(2)
bro.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div/div[1]/ul/li[1]/a').click()
time.sleep(0.5)
bro.find_element_by_xpath('//*[@id="isStudentDan"]/i').click()
time.sleep(2)
bro.find_element_by_id('search_one').click()
time.sleep(2)

到此這篇關(guān)于Selenium之模擬登錄鐵路12306的示例代碼的文章就介紹到這了,更多相關(guān)Selenium 模擬登錄12306內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用Python抓取網(wǎng)頁(yè)數(shù)據(jù)的多種方式與示例詳解

    利用Python抓取網(wǎng)頁(yè)數(shù)據(jù)的多種方式與示例詳解

    在數(shù)據(jù)科學(xué)和網(wǎng)絡(luò)爬蟲領(lǐng)域,網(wǎng)頁(yè)數(shù)據(jù)抓取是非常重要的一項(xiàng)技能,Python 是進(jìn)行網(wǎng)頁(yè)抓取的流行語(yǔ)言,因?yàn)樗鼡碛袕?qiáng)大的第三方庫(kù),能夠簡(jiǎn)化網(wǎng)頁(yè)解析和數(shù)據(jù)提取的過(guò)程,本篇文章將介紹幾種常見(jiàn)的網(wǎng)頁(yè)數(shù)據(jù)抓取方法,需要的朋友可以參考下
    2025-04-04
  • 在Python中將元組轉(zhuǎn)換為列表的方法詳解

    在Python中將元組轉(zhuǎn)換為列表的方法詳解

    這兩種Python 數(shù)據(jù)類型看起來(lái)很相似,但在上下文中卻有不同的用法,元組和列表之間的主要區(qū)別在于它們的可變性,僅當(dāng)您需要修改元素時(shí)才會(huì)將元組轉(zhuǎn)換為列表,本文現(xiàn)在我們將深入研究將元組轉(zhuǎn)換為列表的不同方法,需要的朋友可以參考下
    2023-09-09
  • 超詳細(xì)講解python正則表達(dá)式

    超詳細(xì)講解python正則表達(dá)式

    這篇文章主要介紹了python正則表達(dá)式,利用正則表達(dá)式實(shí)現(xiàn)文本的查找和替換功能會(huì)相對(duì)于比較簡(jiǎn)單,效率也會(huì)更高。感興趣的小伙伴一起來(lái)學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Numpy 理解ndarray對(duì)象的示例代碼

    Numpy 理解ndarray對(duì)象的示例代碼

    這篇文章主要介紹了Numpy 理解ndarray對(duì)象的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python matplotlib繪圖風(fēng)格詳解

    Python matplotlib繪圖風(fēng)格詳解

    從matplotlib的角度來(lái)說(shuō),繪圖風(fēng)格也算是圖像類型的一部分,所以這篇文章小編想帶大家了解一下Python中matplotlib的繪圖風(fēng)格,有需要的可以參考下
    2023-09-09
  • Python判斷字符串是否為空和null方法實(shí)例

    Python判斷字符串是否為空和null方法實(shí)例

    這篇文章主要介紹了Python判斷字符串是否為空和null,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Python數(shù)據(jù)類型轉(zhuǎn)換詳解

    Python數(shù)據(jù)類型轉(zhuǎn)換詳解

    本篇文章里小編給大家整理的是關(guān)于Python中常用數(shù)據(jù)類型之間的轉(zhuǎn)換相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下,希望能夠給你帶來(lái)幫助
    2021-10-10
  • python語(yǔ)言中with as的用法使用詳解

    python語(yǔ)言中with as的用法使用詳解

    本篇文章主要介紹了python語(yǔ)言中with as的用法使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • python flask開(kāi)發(fā)的簡(jiǎn)單基金查詢工具

    python flask開(kāi)發(fā)的簡(jiǎn)單基金查詢工具

    基于python flask開(kāi)發(fā)的簡(jiǎn)單基金查詢工具,支持大盤指數(shù)實(shí)時(shí)情況查看,總持倉(cāng)實(shí)際漲幅、預(yù)估漲幅等功能,感興趣的朋友可以下載該項(xiàng)目來(lái)查看使用
    2021-06-06
  • Python中.py程序在CMD控制臺(tái)以指定虛擬環(huán)境運(yùn)行

    Python中.py程序在CMD控制臺(tái)以指定虛擬環(huán)境運(yùn)行

    本文主要介紹了Python中.py程序在CMD控制臺(tái)以指定虛擬環(huán)境運(yùn)行,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論

北安市| 盘锦市| 彩票| 泸州市| 宝兴县| 巴彦淖尔市| 治多县| 漾濞| 沈阳市| 泽州县| 山丹县| 中山市| 松溪县| 高青县| 迁安市| 金阳县| 唐海县| 长汀县| 河源市| 金华市| 崇阳县| 板桥市| 汤阴县| 双牌县| 岱山县| 永州市| 简阳市| 南澳县| 巫溪县| 全州县| 台东县| 神农架林区| 长宁区| 抚顺县| 衡南县| 沂源县| 乐安县| 疏附县| 大田县| 西畴县| 浦县|