1每天Python小例-12306爬虫#WinError 2
生活随笔
收集整理的這篇文章主要介紹了
1每天Python小例-12306爬虫#WinError 2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注意事項:1.browser[WinError 2]錯誤:找不到文件
下載電腦對應版本的火狐驅動程序geckodriver.exe(我用的是火狐瀏覽器)
2. self.driver = Browser()默認瀏覽器為firefox
3.登錄界面滑動移塊報錯:在self.driver.visit(self.login_url)后加上 :
script = ‘Object.defineProperty(navigator,“webdriver”,{get:()=>undefined,});’
self.driver.execute_script(script)
webdriver必須被設為undefied瀏覽器才不會被判斷為處于自動化中。(網站才能認為是瀏覽器在訪問)
代碼是從github上下載的:
https://github.com/gxcuizy/Python
#!/usr/bin/env python # -*- coding: utf-8 -*-""" 通過splinter刷12306火車票 進入登陸頁面,可以選擇掃碼登陸或者賬號密碼登陸 登陸成功后,接下來的事情,交由腳本來做了,靜靜的等待搶票結果就好(刷票過程中,瀏覽器不可關閉) 搶票成功,會進行手機短信和郵件的通知 author: cuizy time: 2018-11-21 """import re from splinter.browser import Browser from time import sleep import sys import httplib2 from urllib import parse import smtplib from email.mime.text import MIMEText import timeclass BrushTicket(object):"""買票類及實現方法"""def __init__(self, passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile,receiver_email):"""定義實例屬性,初始化"""# 乘客姓名self.passengers = passengers# 起始站和終點站self.from_station = from_stationself.to_station = to_station# 乘車日期self.from_time = from_time# 車次編號self.number = number.capitalize()# 座位類型所在td位置if seat_type == '商務座特等座':seat_type_index = 1seat_type_value = 9elif seat_type == '一等座':seat_type_index = 2seat_type_value = 'M'elif seat_type == '二等座':seat_type_index = 3seat_type_value = 0elif seat_type == '高級軟臥':seat_type_index = 4seat_type_value = 6elif seat_type == '軟臥':seat_type_index = 5seat_type_value = 4elif seat_type == '動臥':seat_type_index = 6seat_type_value = 'F'elif seat_type == '硬臥':seat_type_index = 7seat_type_value = 3elif seat_type == '軟座':seat_type_index = 8seat_type_value = 2elif seat_type == '硬座':seat_type_index = 9seat_type_value = 1elif seat_type == '無座':seat_type_index = 10seat_type_value = 1elif seat_type == '其他':seat_type_index = 11seat_type_value = 1else:seat_type_index = 7seat_type_value = 3self.seat_type_index = seat_type_indexself.seat_type_value = seat_type_value# 通知信息self.receiver_mobile = receiver_mobileself.receiver_email = receiver_email# 新版12306官網主要頁面網址self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html'self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html'self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc'# 瀏覽器驅動信息,驅動下載頁:https://sites.google.com/a/chromium.org/chromedriver/downloadsself.driver_name = 'chrome'self.driver = Browser(driver_name=self.driver_name)def do_login(self):"""登錄功能實現,手動識別驗證碼進行登錄"""self.driver.visit(self.login_url)sleep(1)# 選擇登陸方式登陸print('請掃碼登陸或者賬號登陸……')while True:if self.driver.url != self.init_my_url:sleep(1)else:breakdef start_brush(self):"""買票功能實現"""# 瀏覽器窗口最大化self.driver.driver.maximize_window()# 登陸self.do_login()# 跳轉到搶票頁面self.driver.visit(self.ticket_url)try:print('開始刷票……')# 加載車票查詢信息self.driver.cookies.add({"_jc_save_fromStation": self.from_station})self.driver.cookies.add({"_jc_save_toStation": self.to_station})self.driver.cookies.add({"_jc_save_fromDate": self.from_time})self.driver.reload()count = 0while self.driver.url == self.ticket_url:try:self.driver.find_by_text('查詢').click()except Exception as error_info:print(error_info)sleep(1)continuesleep(0.5)count += 1local_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())print('第%d次點擊查詢……[%s]' % (count, local_date))try:current_tr = self.driver.find_by_xpath('//tr[@datatran="' + self.number + '"]/preceding-sibling::tr[1]')if current_tr:if current_tr.find_by_tag('td')[self.seat_type_index].text == '--':print('無此座位類型出售,已結束當前刷票,請重新開啟!')sys.exit(1)elif current_tr.find_by_tag('td')[self.seat_type_index].text == '無':print('無票,繼續嘗試……')sleep(1)else:# 有票,嘗試預訂print('刷到票了(余票數:' + str(current_tr.find_by_tag('td')[self.seat_type_index].text) + '),開始嘗試預訂……')current_tr.find_by_css('td.no-br>a')[0].click()sleep(1)key_value = 1for p in self.passengers:if '()' in p:p = p[:-1] + '學生' + p[-1:]# 選擇用戶print('開始選擇用戶……')self.driver.find_by_text(p).last.click()# 選擇座位類型print('開始選擇席別……')if self.seat_type_value != 0:self.driver.find_by_xpath("//select[@id='seatType_" + str(key_value) + "']/option[@value='" + str(self.seat_type_value) + "']").first.click()key_value += 1sleep(0.2)if p[-1] == ')':self.driver.find_by_id('dialog_xsertcj_ok').click()print('正在提交訂單……')self.driver.find_by_id('submitOrder_id').click()sleep(2)# 查看放回結果是否正常submit_false_info = self.driver.find_by_id('orderResultInfo_id')[0].textif submit_false_info != '':print(submit_false_info)self.driver.find_by_id('qr_closeTranforDialog_id').click()sleep(0.2)self.driver.find_by_id('preStep_id').click()sleep(0.3)continueprint('正在確認訂單……')self.driver.find_by_id('qr_submit_id').click()print('預訂成功,請及時前往支付……')# 發送通知信息self.send_mail(self.receiver_email, '恭喜您,搶到票了,請及時前往12306支付訂單!')self.send_sms(self.receiver_mobile, '您的驗證碼是:8888。請不要把驗證碼泄露給其他人。')else:print('不存在當前車次【%s】,已結束當前刷票,請重新開啟!' % self.number)sys.exit(1)except Exception as error_info:print(error_info)# 跳轉到搶票頁面self.driver.visit(self.ticket_url)except Exception as error_info:print(error_info)def send_sms(self, mobile, sms_info):"""發送手機通知短信,用的是-互億無線-的測試短信"""host = "106.ihuyi.com"sms_send_uri = "/webservice/sms.php?method=Submit"account = "C59782899"pass_word = "19d4d9c0796532c7328e8b82e2812655"params = parse.urlencode({'account': account, 'password': pass_word, 'content': sms_info, 'mobile': mobile, 'format': 'json'})headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}conn = httplib2.HTTPConnectionWithTimeout(host, port=80, timeout=30)conn.request("POST", sms_send_uri, params, headers)response = conn.getresponse()response_str = response.read()conn.close()return response_strdef send_mail(self, receiver_address, content):"""發送郵件通知"""# 連接郵箱服務器信息host = 'smtp.163.com'port = 25sender = 'gxcuizy@163.com' # 你的發件郵箱號碼pwd = '******' # 不是登陸密碼,是客戶端授權密碼# 發件信息receiver = receiver_addressbody = '<h2>溫馨提醒:</h2><p>' + content + '</p>'msg = MIMEText(body, 'html', _charset="utf-8")msg['subject'] = '搶票成功通知!'msg['from'] = sendermsg['to'] = receivers = smtplib.SMTP(host, port)# 開始登陸郵箱,并發送郵件s.login(sender, pwd)s.sendmail(sender, receiver, msg.as_string())if __name__ == '__main__':# 乘客姓名passengers_input = input('請輸入乘車人姓名,多人用英文逗號“,”連接,(例如單人“張三”或者多人“張三,李四”,如果學生的話輸入“王五()”):')passengers = passengers_input.split(",")while passengers_input == '' or len(passengers) > 4:print('乘車人最少1位,最多4位!')passengers_input = input('請重新輸入乘車人姓名,多人用英文逗號“,”連接,(例如單人“張三”或者多人“張三,李四”):')passengers = passengers_input.split(",")# 乘車日期from_time = input('請輸入乘車日期(例如“2018-08-08”):')date_pattern = re.compile(r'^\d{4}-\d{2}-\d{2}$')while from_time == '' or re.findall(date_pattern, from_time) == []:from_time = input('乘車日期不能為空或者時間格式不正確,請重新輸入:')# 城市cookie字典city_list = {'bj': '%u5317%u4EAC%2CBJP', # 北京'hd': '%u5929%u6D25%2CTJP', # 邯鄲'nn': '%u5357%u5B81%2CNNZ', # 南寧'wh': '%u6B66%u6C49%2CWHN', # 武漢'cs': '%u957F%u6C99%2CCSQ', # 長沙'ty': '%u592A%u539F%2CTYV', # 太原'yc': '%u8FD0%u57CE%2CYNV', # 運城'gzn': '%u5E7F%u5DDE%u5357%2CIZQ', # 廣州南'wzn': '%u68A7%u5DDE%u5357%2CWBZ', # 梧州南}# 出發站from_input = input('請輸入出發站,只需要輸入首字母就行(例如北京“bj”):')while from_input not in city_list.keys():from_input = input('出發站不能為空或不支持當前出發站(如有需要,請聯系管理員!),請重新輸入:')from_station = city_list[from_input]# 終點站to_input = input('請輸入終點站,只需要輸入首字母就行(例如北京“bj”):')while to_input not in city_list.keys():to_input = input('終點站不能為空或不支持當前終點站(如有需要,請聯系管理員!),請重新輸入:')to_station = city_list[to_input]# 車次編號number = input('請輸入車次號(例如“G110”):')while number == '':number = input('車次號不能為空,請重新輸入:')# 座位類型seat_type = input('請輸入座位類型(例如“軟臥”):')while seat_type == '':seat_type = input('座位類型不能為空,請重新輸入:')# 搶票成功,通知該手機號碼receiver_mobile = input('請預留一個手機號碼,方便搶到票后進行通知(例如:18888888888):')mobile_pattern = re.compile(r'^1{1}\d{10}$')while receiver_mobile == '' or re.findall(mobile_pattern, receiver_mobile) == []:receiver_mobile = input('預留手機號碼不能為空或者格式不正確,請重新輸入:')receiver_email = input('請預留一個郵箱,方便搶到票后進行通知(例如:test@163.com):')while receiver_email == '':receiver_email = input('預留郵箱不能為空,請重新輸入:')# 開始搶票ticket = BrushTicket(passengers, from_time, from_station, to_station, number, seat_type, receiver_mobile,receiver_email)ticket.start_brush()經研究短信發送部分使用不了,經過適當修改12306還是可以爬取的,只是爬學生票名字要寫成“張三()"。
搗鼓了一個白天,雖然沒有什么結果,但是挺有趣的。
每天一個小程序,加油!
總結
以上是生活随笔為你收集整理的1每天Python小例-12306爬虫#WinError 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转:以Delphi Package架构多
- 下一篇: C# Environment