def download_by_website(login_url, path_list, base_url, browser_save_path, base_path): b = Browser(driver_name='chrome') b.visit(login_url) b.fill('username', 'hcqlztti') b.fill('password', 'S6rbiowsmhqit?') b.find_by_text('Sign In').click() for path in path_list: curr_url = base_url + path save_path = base_path + path b.visit(curr_url) file_list = b.find_by_id('row').first.find_by_tag( 'tbody').first.find_by_tag('tr') print len(file_list) for item in file_list: file_name = item.find_by_tag('td').first.text print file_name, '++++++++++++++++++' item.find_by_tag('td').first.click() file_path = os.path.join(browser_save_path, file_name) time.sleep(1) if os.path.exists(file_path): shutil.move(file_path, save_path) else: print file_name b.quit()
class hrmis(): username = '******' pwd = 'ESS3BUAT' mainUrl = "http://showcase.aishk.com:7701/HRMIS-UAT2/html/login.jsp" def __init__(self): self.driver_name = 'firefox' self.executable_path = 'E:/driver/geckodriver' self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.visit(self.mainUrl) def login(self): self.driver.fill('opCNA', self.username) self.driver.fill('pwd', self.pwd) self.driver.find_by_name('Submit').last.click() def selectRole(self, role): self.driver.find_by_text(role).last.click() def convertApp(self, url): ''' leave '../leave/index.jsp' ''' self.driver.find_link_by_href(url).last().click()
def login(self): b = Browser(driver_name='chrome') b.visit('https://vpn-guest.citicsinfo.com') b.fill('svpn_name', 'bj-dongsanhuan-1') b.fill('svpn_password', 'EQJtqXC2') b.find_by_text(u'登 录').click() time.sleep(2)
def test_op(): print("hello world!") #browser = Browser("firefox")#模拟浏览器 browser = Browser("chrome")#模拟浏览器 #browser = Browser()#模拟浏览器 browser.visit("http://unptest.xiaobalei.com:5288/gbpayserver/mgr/")#访问指定URL browser.fill("name", "root") browser.fill("pwd", "111111") browser.find_by_value("登陆")[0].click() browser.find_by_text("计费应用管理").click() time.sleep(8) browser.quit()
def huoche(): global b b = Browser()#driver_name="chrome" b.visit(ticket_url) while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: print u"购票页面..." # 跳回购票页面 b.visit(ticket_url) # 加载查询信息 b.cookies.add({"_jc_save_fromStation": starts}) b.cookies.add({"_jc_save_toStation": ends}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() sleep(2) count = 0 # 循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print u"循环点击查询... 第 %s 次" % count sleep(1) try: b.find_by_text(u"预订")[order - 1].click() except: print u"还没开始预订" continue else: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print u"循环点击查询... 第 %s 次" % count sleep(1) try: for i in b.find_by_text(u"预订"): i.click() except: print u"还没开始预订" continue sleep(1) #b.find_by_text(u"更多").click() #sleep(1) #for i in range(len(pa)): b.find_by_text(pa[i])[1].click() print u"能做的都做了.....不再对浏览器进行任何操作" except Exception as e: print(traceback.print_exc())
def huoche(): global b b = Browser(driver_name="chrome") b.visit(ticket_url) while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: print u"购票页面..." # 跳回购票页面 b.visit(ticket_url) # 加载查询信息 b.cookies.add({"_jc_save_fromStation": starts}) b.cookies.add({"_jc_save_toStation": ends}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() sleep(2) count = 0 # 循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() count +=1 print u"循环点击查询... 第 %s 次" % count sleep(1) try: b.find_by_text(u"预订")[order - 1].click() except: print u"还没开始预订" continue else: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print u"循环点击查询... 第 %s 次" % count sleep(1) try: for i in b.find_by_text(u"预订"): i.click() except: print u"还没开始预订" continue sleep(1) b.find_by_text(pa)[1].click() #b.find_by_text(u"提交订单").click() sleep(1) b.find_by_id('qr_submit_id').click() print u"能做的都做了.....不再对浏览器进行任何操作" except Exception as e: print(traceback.print_exc())
def test_op(): print("hello world!") #browser = Browser("firefox")#模拟浏览器 browser = Browser("chrome") #模拟浏览器 #browser = Browser()#模拟浏览器 browser.visit( "http://unptest.xiaobalei.com:5288/gbpayserver/mgr/") #访问指定URL browser.fill("name", "root") browser.fill("pwd", "111111") browser.find_by_value("登陆")[0].click() browser.find_by_text("计费应用管理").click() time.sleep(8) browser.quit()
def huoche(): global b # b这时候变成了全局变量,在huoche()这个函数里面,b产生了变化,那么在下面函数再调用b的时候,那么使用的b则是函数huoche()发生变化了的b,不是之前一开始定义的b b = Browser(driver_name="chrome") b.visit(ticket_url) while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: print(u"购票页面...") # 跳回购票页面 b.visit(ticket_url) # 加载查询信息 b.cookies.add({"_jc_save_fromStation": starts}) b.cookies.add({"_jc_save_toStation": ends}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() sleep(2) count = 0 # 循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第%d次" % (count)) sleep(1) try: b.find_by_text(u"预订")[order - 1].click() except: print(u"还没开始预订") continue else: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %d 次" % (count)) sleep(1) try: for i in b.find_by_text(u"预订"): i.click() except: print(u"还没开始预订") continue sleep(1) for i in range(3): b.find_by_text(pa[i])[0].click() print(u"能做的都做了.....不再对浏览器进行任何操作") except Exception as e: print(traceback.print_exc()) print('all done')
def update_by_website(website_url, sql_file_name): b = Browser(driver_name='chrome') b.visit(website_url) # 登陆 b.fill('username', 'admin') b.fill('password', 'admin') code = b.find_by_id('code')[0].text b.fill('code', code) b.find_by_text('Log in!').click() time.sleep(5)
class TemMail(): def __init__(self): self.driver_name = 'chrome' self.executable_path = '/usr/local/bin/chromedriver' self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(800, 800) self.driver.visit('https://www.moakt.com') self.driver.find_by_name('random').click() while self.driver.url == 'https://www.moakt.com/zh/mail': return def getMailName(self): sleep(1) self.driver.execute_script('changeAddress();') sleep(1) mailname = self.driver.find_by_id('email-address').text return mailname def getEmail(self): while True: self.driver.find_by_text(u"刷新列表").click() temdata = self.driver.find_by_text(u"激活您的ProcessOn帐号") if len(temdata) == 0: sleep(2) else: em = self.driver.find_by_text(u"激活您的ProcessOn帐号").first url = em["href"] + '/content' self.getEmailDea(url) break def getEmailDea(self, emlurl): self.driver.visit(emlurl) temtext = self.driver.html url_verify = re.findall( r"https://www.processon.com/signup/verification/\w+", temtext) ss_mail = requests.Session() rsp_verify = ss_mail.get(url_verify[0]) if rsp_verify.status_code == 200: self.driver.quit() else: print("failure:" + rsp_verify.status_code)
class ProcessOn(): def __init__(self, url): try: self.driver_name = 'chrome' self.executable_path = '/usr/local/bin/chromedriver' self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(800, 800) self.driver.visit(url) self.driver.cookies.delete("processon_userKey") self.driver.find_by_text(u"注册").click() sleep(1) if self.driver.url == 'https://www.processon.com/signup': return except Exception as e: print(e) self.driver.quit() def getrandomstr(self): randomstr = str(random.randint(1000000, 9999999)) return randomstr def signup(self, email): try: self.driver.fill("email", email) self.driver.fill("pass", self.getrandomstr()) self.driver.fill("fullname", self.getrandomstr()) self.driver.find_by_text(u"点击验证").click() print("\r请手动输入验证码") while True: if self.driver.url != 'https://www.processon.com/signup/submit': sleep(1) else: self.driver.cookies.delete() self.driver.quit() break except Exception as e: print(e) self.driver.quit()
def huoche(): global browser browser = Browser(driver_name="chrome") browser.visit(ticket_url) while browser.is_text_present(u"登录"): sleep(1) login() if browser.url == initmy_url: break try: print u"购票页面。。。" # 跳回购票页面 browser.visit(ticket_url) browser.cookies.add({"_jc_save_fromDate": dtime}) # 此处添加出发日期 browser.cookies.add({"_jc_save_fromStation": starts_station}) # 此处添加出发地 browser.cookies.add({u'_jc_save_toStation': ends_station}) # 此处添加目的地 browser.reload() sleep(2) count = 0 if order != 0: while browser.url == ticket_url: browser.find_by_text(u"查询").click() count += 1 print u"循环点击查询。。。 第%s次" % count sleep(1) try: browser.find_by_text(u"预定")[order - 1].click() except: print u"还没开始预订" continue else: while browser.url == ticket_url: browser.find_by_text(u"查询").click() count += 1 print u"循环点击查询... 第 %s 次" % count sleep(1) try: for i in browser.find_by_text(u"预订"): i.click() except: print u"还没开始预订" continue sleep(1) browser.find_by_text(passenger_name)[1].click() print u"能做的都做了.....不再对浏览器进行任何操作" except Exception as e: print traceback.print_exc()
def update_by_website(website_url, sql_file_name): b = Browser(driver_name='chrome') b.visit(website_url) # 登陆 b.fill('username', 'admin') b.fill('password', 'admin') code = b.find_by_id('code')[0].text b.fill('code', code) b.find_by_text('Log in!').click() # 切换至'工具列表'--》'数据库更新' b.find_link_by_href('/tool_list/eod_tools').click() b.find_by_id('sql_file_name').fill(sql_file_name) b.find_by_id('update_database_btn').click() time.sleep(5)
class damai(object): username = "******" password = "******" """网址""" home_url = "https://www.damai.cn/" login_url = "https://www.damai.cn/redirect.aspx?spm=a2o6e.home.0.0.591b48d3J30xXZ&type=login" firstUrl = "" def __init__(self): self.driver_name = 'chrome' self.executable_path = '/usr/local/bin/chromedriver' def start(self): try: self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(800, 800) self.driver.visit(self.home_url) self.driver.cookies.add({"damai.cn_email": "13923497592"}) self.driver.cookies.add({"damai.cn_msgCount": "0"}) self.driver.cookies.add( {"damai.cn_nickName": "%e9%ba%a6%e5%ad%90"}) self.driver.cookies.add({ "damai.cn_user": "******" }) self.driver.cookies.add({ "damai.cn_user_new": "%2f81Gr0X39fNCimPTL4bW1NbITkMd1DpOP%2f7WNLAzdHywTtqzzD77Agmh8mCoA3b1" }) self.driver.cookies.add({ "damai_cn_user": "******" }) self.driver.cookies.add( {"damai_login_QRCode": "6d42297e-0474-45a4-975d-26c64e8def1c"}) self.driver.cookies.add({"damai.cn_email": "13923497592"}) self.driver.cookies.add({ "isg": "BMPDN8Qwnf_rv1GLjCDyMcTGUoGtkFZhK0xvjvWgHiKZtOPWfQjnyqGmKkX6FK9y" }) self.driver.cookies.add({ "x_hm_tuid": "bZmV1v2XlZKb0zVE1LJy1jwY+QX/A8BdLg3bEL11ulfLaI7JI0klBUKra4zP/i4i" }) loginbtn = self.driver.find_by_text("登录").first if (loginbtn): self.login() except BaseException, ex: print("关闭所有由当前测试脚本打开的页面") self.driver.quit()
def huo_che(): global b b = Browser(driver_name="chrome") # 选择其他的浏览器需要换其他的webdriver,webdriver需要设置在环境变量 b.visit(ticket_url) while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: print(u"购票页面...") # 跳回购票页面 b.visit(ticket_url) # 加载查询信息 b.cookies.add({u"_jc_save_fromStation": starts}) b.cookies.add({u"_jc_save_toStation": ends}) b.cookies.add({u"_jc_save_fromDate": d_time}) b.reload() sleep(2) count = 0 # 循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) sleep(1) # 时间间隔可调,但是12306有反爬系统 try: b.find_by_text(u"预订")[order - 1].click() break except Exception as e: print(e) continue else: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) sleep(1) try: for i in b.find_by_text(u"预订"): i.click() except Exception as e: print(e) continue b.find_by_id(u"normalPassenger_0").click() # 这个是选择常用联系人,0代表选的常用联系人列表的第一个乘客 b.find_by_id(u"submitOrder_id").click() sleep(0.5) b.find_by_id(u"qr_submit_id").click() print(u"自行支付") except Exception as e: print(e) print(traceback.print_exc())
def OpenBrowser(username="", password="", start_pos=u"", end_pos=u"", start_time=""): """Open Chorme Browser """ global b b = Browser(driver_name="chrome") b.visit(select_tickets_url) while b.is_text_present(u"登录"): print u"尚未登录,正在登录..." sleep(1) login(username, password) if b.url == index_url: break try: print u"开始购票..." b.visit(select_tickets_url) b.cookies.add({u"_jc_save_fromStation": start_pos}) b.cookies.add({u"_jc_save_toStation": end_pos}) b.cookies.add({u"_jc_save_fromDate": start_time}) b.reload() sleep(2) count = 0 while b.url == select_tickets_url: b.find_by_text(u"查询").click() count += 1 sleep(1) try: for i in b.find_by_text(u"预定"): i.click() except: continue sleep(1) # b.find_by_text() except Exception as e: print (traceback.print_exc())
def initBrowsers(): global browsers global browser_available for i in range(browser_limit): browser_available.append(True) browser_count.append(0) for i in range(browser_limit): try: crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browsers.append(crawler) print("BROWSER " + str(i) + " SET UP") browser_available[i] = True except: print("PROBLEM LOGGING IN... ABORTING") sys.exit() print("A TOTAL OF " + str(len(browsers)) + " BROWSERS WERE PREPARED")
def login(): global browser # insert your own path-to-driver here #executable_path = {'executable_path': 'C:/Users/Jun/Documents/gab/chromedriver.exe'} #browser = Browser('chrome', **executable_path) browser = Browser('firefox', headless=True) browser = getHTML("https://gab.ai/") # It's an account I have already created username = "******" password = "******" browser.find_by_name("username").fill(username) browser.find_by_name("password").fill(password) print(browser.find_by_text("Login")[1].click())
f.close() ticket_url = "https://uems.sysu.edu.cn/elect/s/courses?xkjdszid=2017231005001&fromSearch=false&sid=8eb8baa6-fb6b-45c7-be92-6cfbdbe046bc" login_url = "https://cas.sysu.edu.cn/cas/login?service=http%3A%2F%2Fuems.sysu.edu.cn%2Fjwxt%2Fapi%2Fsso%2Fcas%2Flogin%3Fpattern%3Dstudent-login" initmy_url = "https://uems.sysu.edu.cn/jwxt/#!/student/index" b = Browser("chrome") b.visit(login_url) print(u"等待验证码,自行输入...") while True: if b.url != initmy_url: sleep(1) else: break b.find_by_text(u"选课系统(非体育课)").click() b.find_by_text(u"专选").click() ''' b.reload() sleep(1) count = 0 while b.url == ticket_url: if b.find_by_text(u"查询"): b.find_by_text(u"查询").click() else: reload() continue sleep(3) if b.find_by_text(u"预订"):
class ticket(object): """docstring for 12306抢票""" driver_name = '' executable_path = '' # 用户名,密码 username = u"153****1009" passwd = u"*******" # 车次,选择第几趟,0则从上之下依次点击 order = 1 ###乘客名 users = [u"XXX"] ##席位 xb = u"硬座" pz = u"成人票" """网址""" ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init" login_url = "https://kyfw.12306.cn/otn/login/init" initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc" # 需要下载chromedriver def __init__(self): self.driver_name = 'chrome' self.executable_path = 'D:\Chrome Driver\chromedriver' def login(self): self.driver.visit(self.login_url) # 填充密码 self.driver.fill("loginUserDTO.user_name", self.username) # sleep(1) self.driver.fill("userDTO.password", self.passwd) print(u"等待验证码,自行输入...") while True: if self.driver.url != self.initmy_url: sleep(1) else: break def start(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(1900, 1000) self.login() # sleep(1) self.driver.visit(self.ticket_url) try: print(u"购票页面开始...") # sleep(1) # 加载查询信息 self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() count = 0 if self.order != 0: while self.driver.url == self.ticket_url: try: self.driver.find_by_text(u"查询").click() except Exception as e: continue count += 1 # 根据td的id获取节点以判断是否有票 id需要手工获取 isTicketElement = self.driver.find_by_id("YW_490000502609") if len(isTicketElement) == 0: print("网络错误") continue print(u"循环查询... 第 %s 次,还是木有票" % count) isTicket = self.driver.find_by_id( "YW_490000502609")._element.text sleep(30) # isTicket = "--" or isTicket = "无" 均是指无相应席位的余票 if isTicket == "无": continue # sleep(1) try: self.driver.find_by_text(u"预订")[self.order - 1].click() continue except Exception as e: print(e) print(u"还没开始预订") continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 isTicket = self.driver.find_by_id("YW_490000502609") print(isTicket) sleep(0.8) try: for i in self.driver.find_by_text(u"预订"): i.click() sleep(1) except Exception as e: print(e) print(u"还没开始预订 %s" % count) continue print(u"开始预订...") # sleep(3) # self.driver.reload() sleep(1) print(u'开始选择用户...') for user in self.users: self.driver.find_by_text(user).last.click() # seatType_1 选择席位 略 print(u"提交订单...") sleep(1) # 选择席别、票种示例 # self.driver.find_by_xpath('//select[@id="seatType_1"]/option[@value="0"]')._element.click() # self.driver.find_by_text(self.pz).click() # self.driver.find_by_id('').select(self.pz) # # sleep(1) # self.driver.find_by_text(self.xb).click() # sleep(1) self.driver.find_by_id('submitOrder_id').click() # print u"开始选座..." # self.driver.find_by_id('1D').last.click() # self.driver.find_by_id('1F').last.click() sleep(1.5) print(u"确认选座...") self.driver.find_by_id('qr_submit_id').click() except Exception as e: print(e)
class huoche(object): driver_name = '' executable_path = '' #用户名,密码 username = u"xxx" passwd = u"xxx" # cookies值得自己去找, 下面两个分别是沈阳, 哈尔滨 starts = u"%u6C88%u9633%2CSYT" ends = u"%u54C8%u5C14%u6EE8%2CHBB" # 时间格式2018-01-19 dtime = u"2018-01-19" # 车次,选择第几趟,0则从上之下依次点击 order = 0 ###乘客名 users = [u"xxx", u"xxx"] ##席位 xb = u"二等座" pz = u"成人票" """网址""" ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init" login_url = "https://kyfw.12306.cn/otn/login/init" initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc" def __init__(self): self.driver_name = 'chrome' self.executable_path = 'D:/chromedriver' def login(self): self.driver.visit(self.login_url) self.driver.fill("loginUserDTO.user_name", self.username) # sleep(1) self.driver.fill("userDTO.password", self.passwd) print(u"等待验证码,自行输入...") while True: if self.driver.url != self.initmy_url: sleep(1) else: break def start(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(1400, 1000) self.login() # sleep(1) self.driver.visit(self.ticket_url) try: print(u"购票页面开始...") # sleep(1) # 加载查询信息 self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() count = 0 if self.order != 0: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) # sleep(1) try: self.driver.find_by_text(u"预订")[self.order - 1].click() except Exception as e: print(e) print(u"还没开始预订") continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) # sleep(0.8) try: for i in self.driver.find_by_text(u"预订"): i.click() sleep(1) except Exception as e: print(e) print(u"还没开始预订 %s" % count) continue print(u"开始预订...") # sleep(3) # self.driver.reload() sleep(1) print(u'开始选择用户...') for user in self.users: self.driver.find_by_text(user).last.click() print(u"提交订单...") sleep(1) self.driver.find_by_text(self.pz).click() self.driver.find_by_id('').select(self.pz) # sleep(1) self.driver.find_by_text(self.xb).click() sleep(1) self.driver.find_by_id('submitOrder_id').click() print(u"开始选座...") self.driver.find_by_id('1D').last.click() self.driver.find_by_id('1F').last.click() sleep(1.5) print(u"确认选座...") self.driver.find_by_id('qr_submit_id').click() except Exception as e: print(e)
#b.fill('wd','splinter')#通过chrome的F12可以看到输入框的name=wd,fill的内容是splinter #button=b.find_by_value(u'百度一下')#百度搜索栏的value=”百度一下”,id=”su” #button=b.find_by_id(u'su') #button.click()#模拟点击百度一下 #if b.is_text_present('splinter.cobrateam.info'): # print 'True' #else: # b.quit() username=u'*****@*****.**' passwd=u'020002118_' zz=u'%u67A3%u5E84%2CZEK' sh=u'%u4E0A%u6D77%2CSHH' dtime=u'2016-02-14' order=0 passenger=u'钟啸林' b.find_by_text(u'登录').click()#这里的by text和前面的by value,by id差不多 sleep(3) b.fill("loginUserDTO.user_name",username) b.fill("userDTO.password",passwd)#这里需要通过F12来寻找<input中的name对应的 print u'wait for you to input the 验证码....' while True: if b.url!=initmy_url: sleep(1) else: break print(u'跳回购票页面....') b.visit(ticket_url) b.cookies.add({'_jc_save_fromStation':zz}) b.cookies.add({'_jc_save_toStation':sh}) b.cookies.add({'_jc_save_fromDate':dtime})
def MakeNetwork(): global bot global nonbot #global seen global primary_count global browser # cheat a little... cut the bot checking wait... global metacursor global metacon config = { 'user': "******", 'password': '******', 'host': 'localhost', 'database': 'Meta', 'raise_on_warnings': True, 'charset': 'utf8mb4', 'collation': 'utf8mb4_bin' } metacon = mysql.connector.connect(**config) metacursor = metacon.cursor(buffered=True) readBots = "Select name from bots" botusers = DBexecuter("select", readBots, None, metacursor, metacon) for b in botusers: botuser = b[0].decode("utf-8") #seen.add(botuser) MakeSeen(botuser) AddBot(botuser) #bot.add(botuser) print("Added to botlist from DB: {}".format(botuser)) #readUsers = "Select User.username from User left join Follows on User.username = Follows.followee where Follows.Follower is null order by User.followers desc" readUsers = "Select username from User" #readUsers = "Select `User`.`username` From User Order By User.Followers DESC" #readUsers = "Select A.Followee, (`User`.`followers` - A.counts) as Dif from (select Followee, count(*) as counts from Follows Group by Follows.Followee) as A join User on User.username = A.Followee order by Dif DESC" users = DBexecuter("select", readUsers, None, cursor, cnx) q = queue.Queue() for u in users: user = u[0].decode("utf-8") #seen.add(user) MakeSeen(user) nonbot.add(user) q.put(user) print("Added to network from DB: {}".format(user)) global testcounter while not q.empty(): # ensure we haven't gone over browse limit primary_count += 1 if primary_count >= BROWSE_LIMIT: try: browser.quit() crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browser = crawler primary_count = 0 print("PRIMARY BROWSER RESET") except: print("failed to reset primary browser") user = q.get() # known not to be bot testcounter += 1 print("\nSCRAPING " + user) print("grabbing followings") followings = AddNeighbors(user, False) # NOTE: THIS FILTERS BOTS ON ITS OWN print("grabbing followers") followers = AddNeighbors(user, True) print("found {} following".format(len(followings))) print("found {} followers".format(len(followers))) for f_raw in (followings + followers): try: f = f_raw.text[1:] if not UserSeen(f): # if user not yet seen .. IF SLOW, WE CAN MAKE THIS IF NOT USERINDB print("\nappending user {} to the queue...".format(f)) q.put(f) MakeSeen(f) print("there are now {} members in the queue".format(len(q))) except: print("error adding user to queue") continue
class Buy_Tickets(object): # 初始化 def __init__(self, username, passwd, order, passengers, dtime, starts, ends): self.url_template = ( 'https://kyfw.12306.cn/otn/leftTicket/query{}?leftTicketDTO.' 'train_date={}&' 'leftTicketDTO.from_station={}&' 'leftTicketDTO.to_station={}&' 'purpose_codes=ADULT' ) self.username = username self.passwd = passwd # 日期 self.dtime = dtime # 乘客名 self.passengers = passengers.split(',') # 起始地和终点 self.starts = Station_Parse.parse_station().parse(starts) self.ends = Station_Parse.parse_station().parse(ends) if self.starts is None or self.ends is None: self.title = '提示' self.message = '请输入有效的车站名' messagebox.showinfo(self.title, self.message) # 车次 self.order = order if self.order != '0': self.order = self.order_transfer() while(not self.order): sleep(1) self.order = self.order_transfer() # 起始地和终点转为cookie值 self.starts = self.Get_Cookies()[0] + '%2C' + self.starts self.ends = self.Get_Cookies()[1] + '%2C' + self.ends self.login_url = 'https://kyfw.12306.cn/otn/login/init' self.initMy_url = 'https://kyfw.12306.cn/otn/index/initMy12306' self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init' self.driver_name = 'chrome' self.executable_path = 'D:\\Python35\\Scripts\\chromedriver.exe' # 登录 def login(self): self.driver.visit(self.login_url) self.driver.fill('loginUserDTO.user_name', self.username) self.driver.fill('userDTO.password', self.passwd) self.title = '提示' self.message = '请在自动打开的浏览器中输入验证码!' messagebox.showinfo(self.title, self.message) while True: if self.driver.url != self.initMy_url: sleep(1) else: break # 车次转换 def order_transfer(self): self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3294.6 Safari/537.36'} self.res = requests.get(self.url_template.format('A', self.dtime, self.starts, self.ends), headers=self.headers, verify=False) try: try: self.trains = self.res.json()['data']['result'] except: self.res = requests.get(self.url_template.format('Z', self.dtime, self.starts, self.ends), headers=self.headers, verify=False) self.trains = self.res.json()['data']['result'] self.num = 0 for self.train in self.trains: self.data_list = self.train.split('|') if self.data_list[3] == self.order: break self.num += 1 if self.num == len(self.trains): self.title = '提示' self.message = '您输入的车次不存在,系统将自动选择任意车次!' messagebox.showinfo(self.title, self.message) self.num = '0' return self.num except: title = '提示' message = '因为网络原因正在重新尝试!' messagebox.showinfo(title, message) return None # 中文转Unicode def to_unicode(self, string): self.uni = '' for s in string: self.uni += hex(ord(s)).upper().replace('0X', '%u') return self.uni # 将起始地和终点转化为相应的Cookies def Get_Cookies(self): return [self.to_unicode(self.starts), self.to_unicode(self.ends)] # 买票 def start_buy(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(700, 500) self.login() self.driver.visit(self.ticket_url) try: self.title = '提示' self.message = '开始购票!' messagebox.showinfo(self.title, self.message) # 加载查询信息 self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() if self.order != '0': while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() # self.title = '提示' # self.message = '开始尝试预订...' # messagebox.showinfo(self.title, self.message) try: self.driver.find_by_text('预订')[self.order].click() sleep(1.5) except: # self.title = '提示' # self.message = '预订失败...' # messagebox.showinfo(self.title, self.message) continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() # self.title = '提示' # self.message = '开始尝试预订...' # messagebox.showinfo(self.title, self.message) try: for i in self.driver.find_by_text('预订'): i.click() sleep(1) except: # self.title = '提示' # self.message = '预订失败...' # messagebox.showinfo(self.title, self.message) continue self.title = '提示' self.message = '开始选择用户~~~' messagebox.showinfo(self.title, self.message) sleep(1) for p in self.passengers: self.driver.find_by_text(p).last.click() sleep(0.5) if p[-1] == ')': self.driver.find_by_id('dialog_xsertcj_ok').click() self.title = '提示' self.message = '提交订单中...' messagebox.showinfo(self.title, self.message) sleep(1) self.driver.find_by_id('submitOrder_id').click() sleep(2) self.title = '提示' self.message = '确认选座中...' messagebox.showinfo(self.title, self.message) self.driver.find_by_id('qr_submit_id').click() self.title = '提示' self.message = '预订成功...' messagebox.showinfo(self.title, self.message) self.content = '恭喜您抢票成功,请在半小时内完成支付!!!' _ = os.system('mshta vbscript:createobject("sapi.spvoice").speak("%s")(window.close)' % self.content) except: self.title = '提示' self.message = '出现了一点小错误,可能是输入信息有误导致的...' messagebox.showinfo(self.title, self.message)
print len(file_list) for item in file_list: file_name = item.find_by_tag('td').first.text print file_name, '++++++++++++++++++' item.find_by_tag('td').first.click() file_path = os.path.join(browser_save_path, file_name) time.sleep(1) if os.path.exists(file_path): shutil.move(file_path, save_path) else: print file_name b.quit() if __name__ == '__main__': # browser_save_path = 'C:/Users/wt/Downloads' # base_path = 'D:/work/barra/' # login_url = 'https://fileservice.msci.com/' # base_url = 'https://fileservice.msci.com/m/home/hcqlztti/barra/' # path_list = ['cne5/', '/cne5/model_receipt/', '/cne5/daily/', '/bime/'] # download_by_website(login_url, path_list, base_url, browser_save_path, base_path) # shutil.rmtree('D:/work/download/') # shutil.move(browser_save_path, 'D:/work/barra/cne5/') b = Browser(driver_name='chrome') b.visit('https://vpn-guest.citicsinfo.com') b.fill('svpn_name', 'bj-dongsanhuan-1') b.fill('svpn_password', 'EQJtqXC2') b.find_by_text(u'登 录').click() time.sleep(2) b.find_by_id('sendSms').click()
d.find_by_text('内场1355').click() d.find_by_id('btnBuyNow').click() time.sleep(5) order_confirm(d) def payment(d): if d.title == '选择支付方式': d.find_by_text('支付平台付款').click() d.find_by_value('213').first.click() time.sleep(1) d.find_by_id('submit2').click() else: d.reload() order_confirm(d) payment(d) d = Browser(driver_name='chrome') login(d) time.sleep(5) d.visit(url_ticket) d.find_by_text('内场1355').click() time.sleep(4) d.find_by_id('btnBuyNow').click() order_confirm(d) payment(d) time.sleep(3) if d.title == '支付宝 - 网上支付 安全快速!': print('SUCCESS!') else: print('RETRY PLEASE!')
#-*- coding: UTF-8 -*- from splinter.browser import Browser b = Browser(driver_name="chrome") # b.visit("http://www.baidu.com") # b.fill("wd", "splinter") # button = b.find_by_value(u"百度一下") # button.click() b.visit("https://kyfw.12306.cn/otn/login/init") b.find_by_text(u"登录").click() username="******" passwd="12306_com_" b.fill("loginUserDTO.user_name", username) # username部分输入自己的账号 b.fill("userDTO.password", passwd) # passwd部分输入账号密码 b.cookies.add({"_jc_save_fromDate":"2016-02-13"}) # 此处添加出发日期 b.cookies.add({"_jc_save_fromStation":"%u6210%u90FD%2CCDW"}) # 此处添加出发地 b.cookies.add({u'_jc_save_toStation':'%u676D%u5DDE%2CHZH'}) # 此处添加目的地 b.cookies.all() b.find_by_text(u"查询").click() b.find_by_text(u"预订")[1].click() # 下标1表示买的是可选班次的第二班车
def check(): global b b = Browser(driver_name="chrome") b.visit(ticket_url) b.execute_script('alert("开始刷票")') sleep(2) b.get_alert().dismiss() while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: # 跳回购票页面 b.visit(ticket_url) # 加载车票查询信息 b.cookies.add({"_jc_save_fromStation":starts}) b.cookies.add({"_jc_save_toStation":ends}) b.cookies.add({"_jc_save_fromDate":dtime}) b.reload() i = 1 # 循环点击预订 if order != 0: while b.url == ticket_url: sleep(3) b.find_by_text(u"查询").click() # b.find_by_text(ttype).click() if b.find_by_text(u"预订"): sleep(0.3) b.find_by_text(u"预订")[order - 1].click() print(b.url) if b.is_text_present(u"证件号码",wait_time = 0.5): # print [ i.text for i in b.find_by_text(pa) ] b.find_by_text(pa)[1].click() else: b.execute_script('alert("没有可预订选项")') b.get_alert().dismiss() pass else: while b.url == ticket_url: sleep(3) b.find_by_text(u"查询").click() if b.find_by_text(u"预订"): sleep(0.3) for i in b.find_by_text(u"预订"): i.click() sleep(0.1) if b.is_text_present(u"证件号码"): b.find_by_text(pa)[1].click() else: b.execute_script('alert("似乎没有可预订选项")') b.get_alert().dismiss() pass b.execute_script('alert("能做的都做了")') b.get_alert().dismiss() print(u"能做的都做了.....不再对浏览器进行任何操作") except Exception: print(u"出错了....")
from splinter.browser import Browser b = Browser(driver_name="chrome") url = 'https://kyfw.12306.cn/otn/leftTicket/init' b = Browser(driver_name="chrome") b.visit(url) b.find_by_text(u"登录").click() b.fill("loginUserDTO.user_name","xxxx") b.fill("userDTO.password","xxxx") b.cookies.all() {u'BIGipServerotn': u'1977155850.38945.0000', u'JSESSIONID': u'0A01D97598F459F751C4AE8518DBFB300DA7001B67', u'__NRF': u'95D48FC2E0E15920BFB61C7A330FF2AE', u'_jc_save_fromDate': u'2018-02-22', u'_jc_save_fromStation': u'%e5%b0%9a%e5%bf%97', u'_jc_save_toStation': u'%e6%9d%ad%e5%b7%9e', u'current_captcha_type': u'Z'} b.cookies.add({"_jc_save_fromStation":"%e5%b0%9a%e5%bf%97"}) 添加出发日期 b.cookies.add({"_jc_save_fromDate":"2018-02-22"}) 添加目的地 b.cookies.add({u'_jc_save_toStation':'%e6%9d%ad%e5%b7%9e'}) b.reload() b.find_by_text(u"查询").click() autoSearchTime=1000
class Ticket(object): def __init__(self, config_file): ## config parser setting self.config_file = config_file self.settings = configparser.ConfigParser() self.settings._interpolation = configparser.ExtendedInterpolation() self.settings.read(self.config_file,'utf-8') ## environment setting self.brower='chrome' self.b = Browser(driver_name=self.brower) self.station={} self.url = "https://kyfw.12306.cn/otn/leftTicket/init" # 席别类型(对应列标号) self.ticket_index = [ '', u'商务座', u'特等座', u'一等座', u'二等座', u'高级软卧', u'软卧', u'硬卧', u'软座', u'硬座', u'无座' ] self.username = '' self.password = '' self.date_format='%Y-%m-%d' self.tolerance = -1 self.blacklist = {} self.date = [] self.isStudent = False self.success = 0 self.find_ticket = 0 self.config_parser() self.playmusic = False self.count = 0 def ConfigSectionMap(self,section): dict1 = {} options = self.settings.options(section) for option in options: try: dict1[option] = self.settings.get(section, option) if dict1[option] == -1: DebugPrint("skip: %s" % option) except: print("exception on %s!" % option) dict1[option] = None return dict1 def daterange(self, start_date, end_date): for n in range(int ((end_date - start_date).days) + 1): yield start_date + timedelta(n) def config_parser(self): if self.retrieve_station_dict() == -1: sys.exit() if self.retrieve_book_options() == -1: sys.exit() def retrieve_station_dict(self): dict_helper=self.ConfigSectionMap('STATIONCOOKIE') for name, value in dict_helper.iteritems(): self.station[name]=value def retrieve_book_options(self): login_info=self.ConfigSectionMap('GLOBAL') self.username = login_info['username'].strip() self.password = login_info['password'].strip() self.brower = login_info['browser'] book_settings = self.ConfigSectionMap('TICKET') self.fromStation = [ station.strip() for station in book_settings['from_station'].split(',')] self.toStation = [ station.strip() for station in book_settings['to_station'].split(',')] trains = [ train.strip() for train in book_settings['trains'].split(',')] if len(trains) == 1 and trains[0] == '': self.trains = [] else: self.trains = trains self.ticket_type =[ _type.strip() for _type in book_settings['ticket_type'].split(',')] rangeQuery = book_settings['range_query'].strip() if rangeQuery == 'Y': date = [ d.strip() for d in book_settings['date'].split(',')] if len(date) < 2: print "未设置正确的起至时间" return -1 else: start_date = datetime.strptime(date[0],self.date_format) end_date = datetime.strptime(date[1],self.date_format) if end_date < start_date: print "查询截止日期不可大于开始日期!" return -1 for single_date in self.daterange(start_date, end_date): self.date.append(single_date.strftime(self.date_format)) else: self.date = [ d.strip() for d in book_settings['date'].split(',')] if book_settings['student'].strip() == 'Y': self.isStudent = True self.tolerance = int(book_settings['tolerance']) self.people = [ people.strip() for people in book_settings['people'].split(',') ] if book_settings['alarm'].strip() == 'Y': self.playmusic = True def login(self): self.b.visit(self.url) self.b.find_by_text(u"登录").click() self.b.fill("loginUserDTO.user_name",self.username) self.b.fill("userDTO.password",self.password) pdb.set_trace() def page_has_loaded(self): page_state = self.b.evaluate_script("document.readyState") return page_state == 'complete' def switch_to_order_page(self): order = [] while len(order) == 0: order = self.b.find_by_text(u"车票预订") order[0].click() def checkTicket(self, date, fromStation, toStation): print 'date: %s, from %s, to %s'%(date, fromStation, toStation) self.b.cookies.add({"_jc_save_fromDate":date}) self.b.cookies.add({"_jc_save_fromStation":self.station[fromStation]}) self.b.cookies.add({"_jc_save_toStation":self.station[toStation]}) self.b.cookies.all() self.b.reload() if self.isStudent: self.b.find_by_text(u'学生').click() self.b.find_by_text(u"查询").click() all_trains = [] while self.page_has_loaded() == False: continue while len(all_trains) == 0: all_trains = self.b.find_by_id('queryLeftTable').find_by_tag('tr') for k, train in enumerate(all_trains): tds = train.find_by_tag('td') if tds and len(tds) >= 10: has_ticket= tds.last.find_by_tag('a') if len(has_ticket) != 0: if k + 1 < len(all_trains): this_train = all_trains[k+1]['datatran'] if len(self.trains) != 0 and this_train not in self.trains: continue if self.tolerance != -1 and this_train in self.blacklist and self.blacklist[this_train] >= self.tolerance: print u"%s 失败 %d 次, 跳过"%(this_train, self.blacklist[this_train]) continue for cat in self.ticket_type: if cat in self.ticket_index: i = self.ticket_index.index(cat) else: print '无效的席别信息' return 0, '' if tds[i].text != u'无' and tds[i].text != '--': if tds[i].text != u'有': print u'%s 的 %s 有余票 %s 张!'%(this_train, cat ,tds[i].text) else: print u'%s 的 %s 有余票若干张!'%(this_train, cat) self.find_ticket = 1 tds.last.click() break if self.find_ticket: break return this_train def book(self): while self.page_has_loaded() == False: continue if len(self.people) == 0: print '没有选择乘车人!' return 1 more = [] while len(more) == 0: more = self.b.find_by_text(u"更多") more[0].click() person = [] people = self.people while len(person) == 0: person=self.b.find_by_xpath('//ul[@id="normal_passenger_id"]/li/label[contains(text(),"%s")]'%people[0]) for p in people: self.b.find_by_xpath('//ul[@id="normal_passenger_id"]/li/label[contains(text(),"%s")]'%p).click() if self.b.find_by_xpath('//div[@id="dialog_xsertcj"]').visible: self.b.find_by_xpath('//div[@id="dialog_xsertcj"]/div/div/div/a[text()="确认"]').click() return 1 def ring(self): pygame.mixer.pre_init(64000, -16, 2, 4096) pygame.init() pygame.display.init() screen=pygame.display.set_mode([300,300]) #pygame.display.flip() pygame.time.delay(1000)#等待1秒让mixer完成初始化 tracker=pygame.mixer.music.load("media/sound.ogg") #track = pygame.mixer.music.load("sound.ogg") pygame.mixer.music.play() # while pygame.mixer.music.get_busy(): #pygame.time.Clock().tick(10) running = True img=pygame.image.load("media/img.jpg") while running: screen.blit(img,(0,0)) pygame.display.flip() for event in pygame.event.get(): if event.type==pygame.QUIT: running = False pygame.quit () return 1 def executor(self): self.login() self.switch_to_order_page() while self.success == 0: self.find_ticket = 0 while self.find_ticket == 0: for date in self.date: try: self.count += 1 print "Try %d times" % self.count for fromStation in self.fromStation: for toStation in self.toStation: this_train = self.checkTicket(date, fromStation, toStation) if self.find_ticket: break if self.find_ticket: break if self.find_ticket: break except KeyboardInterrupt: self.b.find_by_text(u'退出').click() sys.exit() except: continue # book ticket for target people self.find_ticket = 0 while self.find_ticket == 0: try: self.find_ticket = self.book() except KeyboardInterrupt: self.b.find_by_text(u'退出').click() sys.exit() except: continue if self.playmusic: self.ring() print "订票成功了吗?(Y/N)" input_var = '' while input_var == '': input_var= sys.stdin.read(1) if input_var == 'Y' or input_var == 'y': self.success = 1 elif input_var == 'N' or input_var == 'n': if this_train in self.blacklist: self.blacklist[this_train] += 1 else: self.blacklist[this_train] = 1 print u"%s 失败 %d 次"%(this_train, self.blacklist[this_train]) self.b.back() self.b.reload() else: input_var = '' continue self.b.find_by_text(u'退出').click()
def huoche(): ends = "%u5357%u660C%2CNCG" global b b = Browser(driver_name="chrome") # b = Browser() b.visit(ticket_url) # b.execute_script('alert("开始刷票喽~~~~")') # sleep(2) # b.get_alert().dismiss() while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: #跳回购票页面 b.visit(ticket_url) #加载查询信息 b.cookies.add({"_jc_save_fromStation":starts}) b.cookies.add({"_jc_save_toStation":ends}) b.cookies.add({"_jc_save_fromDate":starttime}) b.cookies.add({"_jc_save_toDate":endtime}) b.reload() i = 1 b.find_by_name(ttype)[4].click() stations = b.find_by_name(fromStation) if stations: stations[0].click() else: print "no stations can select" #循环点击预订 if order != 0: while b.url == ticket_url: print "execute loop order != 0" b.find_by_text(u"查询").click() if b.find_by_text(u"预订"): sleep(0.3) b.find_by_text(u"预订")[order - 1].click() print b.url if b.is_text_present(u"证件号码",wait_time=0.2): # print [ i.text for i in b.find_by_text(pa) ] b.find_by_id("normalPassenger_0").click() b.find_by_id("normalPassenger_1").click() else: # b.execute_script('alert("似乎没有可预订选项")') # b.get_alert().dismiss() print "似乎没有可预订选项" pass else: while b.url == ticket_url: print "execute loop order == 0" b.find_by_text(u"查询").click() if b.find_by_text(u"预订"): sleep(0.3) for i in b.find_by_text(u"预订"): i.click() sleep(0.1) if b.is_text_present(u"证件号码"): # b.find_by_text(pa)[1].click() b.find_by_id("normalPassenger_0").click() b.find_by_id("normalPassenger_1").click() # b.find_by_text("李芷嫣")[0].click() else: # b.execute_script('alert("似乎没有可预订选项")') # b.get_alert().dismiss() print u"似乎没有可预订选项" pass else: print "can't find orderitem" # b.execute_script('alert("能做的都做了")') # b.get_alert().dismiss() print u"能做的都做了.....不再对浏览器进行任何操作" except Exception as e: print u"出错了....%s"%e
class BrushTicket(object): """买票类及实现方法""" def __init__(self, passengers_number): """定义实例属性,初始化""" cp = ConfigParser() cp.read("conf/city.conf", encoding='UTF-8') sections = cp.sections() city = sections[0] key_list = cp.options(city) self.city_dict = {} for key in key_list: self.city_dict[key] = cp.get(city, key) cp = ConfigParser() cp.read("conf/12306.conf", encoding='UTF-8') sections = cp.sections() pessenger = sections[passengers_number] # 乘客姓名 self.passengers = cp.get(pessenger, 'name') # 起始站和终点站 self.from_station = self.city_dict[cp.get(pessenger, 'from_station')] self.to_station = self.city_dict[cp.get(pessenger, 'to_station')] # 乘车日期 self.from_time = cp.get(pessenger, 'from_time') # 车次编号 self.number = cp.get(pessenger, 'coach_number') seat_type = cp.get(pessenger, 'seat_type') # 座位类型所在td位置 if seat_type == '商务座特等座': seat_type_index = 1 seat_type_value = 9 elif seat_type == '一等座': seat_type_index = 2 seat_type_value = 'M' elif seat_type == '二等座': seat_type_index = 3 seat_type_value = 0 elif seat_type == '高级软卧': seat_type_index = 4 seat_type_value = 6 elif seat_type == '软卧': seat_type_index = 5 seat_type_value = 4 elif seat_type == '动卧': seat_type_index = 6 seat_type_value = 'F' elif seat_type == '硬卧': seat_type_index = 7 seat_type_value = 3 elif seat_type == '软座': seat_type_index = 8 seat_type_value = 2 elif seat_type == '硬座': seat_type_index = 9 seat_type_value = 1 elif seat_type == '无座': seat_type_index = 10 seat_type_value = 1 elif seat_type == '其他': seat_type_index = 11 seat_type_value = 1 else: seat_type_index = 7 seat_type_value = 3 self.seat_type_index = seat_type_index self.seat_type_value = seat_type_value # 通知信息 # self.receiver_mobile = receiver_mobile self.receiver_email = cp.get(pessenger, '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/downloads self.driver_name = 'chrome' self.driver = Browser(driver_name=self.driver_name) def do_login(self): """登录功能实现,识别验证码 可以使用12306APP 扫一扫登录""" self.driver.visit(self.login_url) sleep(1) # 选择登陆方式登陆 print('请扫码登陆或者账号登陆……') while True: if self.driver.url != self.init_my_url: sleep(1) else: break def start_brush(self): """买票功能实现""" # 浏览器窗口最大化 self.driver.driver.maximize_window() # 登陆 self.do_login() # 跳转到抢票页面 self.driver.visit(self.ticket_url) try: print('开始刷票……') title_start = '开始抢票' # self.send_mail(self.receiver_email, '开始抢票' + self.from_time + # '的车票,请关注邮箱抢票通知', title_start) # 加载车票查询信息 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 = 0 while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() sleep(1) count += 1 print('第%d次点击查询……' % count) 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 = 1 for p in self.passengers: # 选择用户 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 += 1 sleep(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].text if 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) continue print('正在确认订单……') self.driver.find_by_id('qr_submit_id').click() print('预订成功,请及时前往支付……') # 发送通知信息 title = '抢票票成功!!' self.send_mail( self.receiver_email, '恭喜您,抢到了' + self.from_time + '的车票,请及时前往12306支付订单!', title) else: print('不存在当前车次【%s】,已结束当前刷票,请重新开启!' % self.number) sys.exit(1) except Exception as error_info: print(error_info) except Exception as error_info: print(error_info) def send_mail(self, receiver_address, content, title): mail_host = 'smtp.163.com' # 163用户名 mail_user = '******' # 密码(部分邮箱为授权码) mail_pass = '******' # 邮件发送方邮箱地址 sender = '*****@*****.**' # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发 receivers_list = ['*****@*****.**', receiver_address] email = Email(mail_host, mail_user, mail_pass) for receiver in receivers_list: # 设置email信息 # 邮件内容设置 smtpObj = email.getConnection() message = MIMEText(str(content), 'plain', 'utf-8') # 邮件主题 message['Subject'] = title # 发送方信息 message['From'] = sender # 接受方信息 message['To'] = receiver email.send_email(smtpObj, sender, receiver, message)
def huoche(): global b b = Browser(driver_name="chrome") b.visit(ticket_url) b.execute_script('alert("开始刷票喽~~~~")') sleep(2) b.get_alert().dismiss() while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: #跳回购票页面 b.visit(ticket_url) #加载查询信息 b.cookies.add({"_jc_save_fromStation": starts}) b.cookies.add({"_jc_save_toStation": ends}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() i = 1 #循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() # b.find_by_text(ttype).click() if b.find_by_text(u"预订"): sleep(0.3) b.find_by_text(u"预订")[order - 1].click() print b.url if b.is_text_present(u"证件号码", wait_time=0.2): # print [ i.text for i in b.find_by_text(pa) ] b.find_by_text(pa)[1].click() else: b.execute_script('alert("似乎没有可预订选项")') b.get_alert().dismiss() pass else: while b.url == ticket_url: b.find_by_text(u"查询").click() if b.find_by_text(u"预订"): sleep(0.3) for i in b.find_by_text(u"预订"): i.click() sleep(0.1) if b.is_text_present(u"证件号码"): b.find_by_text(pa)[1].click() else: b.execute_script('alert("似乎没有可预订选项")') b.get_alert().dismiss() pass b.execute_script('alert("能做的都做了")') b.get_alert().dismiss() print u"能做的都做了.....不再对浏览器进行任何操作" except Exception: print u"出错了...."
from splinter.browser import Browser print "hello twitter" handle = raw_input('twitter handle:') #pw = raw_input('password:'******'firefox') browser.visit('https://twitter.com/') loginbutton = browser.find_by_text('Log In') loginbutton.first.click() #works up till here usernamebox = browser.find_by_id('signin-email') #passwordbox = browser.find_by_id('signin-password') if len(usernamebox)==0: print "no username boxes found =^[" browser.fill(usernamebox.first, handle) #browser.fill(passwordbox.first, pw)
class Scrapy12306(): def __init__(self): config = ConfigParser.ConfigParser() config.readfp(codecs.open("12306.conf", "r", "utf-8")) self.url = config.get("config","url") self.username = config.get("config","username") self.password = config.get("config","password") self.bType = config.get("config","browser") #self._from = config.get("config","_from") #self._to = config.get("config","_to") self._from = u'北京' self._to = u'邯郸' self.date = config.get("config","date") print "browser :%s url:%s" % (self.bType,self.url) self.browser = Browser(self.bType) self.browser.visit(self.url) def login(self): if self.username=="" or self.password=="": print "error:username and password must not be empty" self.browser.quit() sys.exit(-1) self.browser.fill("loginUserDTO.user_name",self.username) self.browser.fill("userDTO.password",self.password) time.sleep(10) self.browser.find_by_id("loginSub").click() loginFlag = self.browser.find_by_id("link_4_myTicket") return not loginFlag.is_empty() def getTicket(self): self.browser.find_by_xpath("//a[@href='/otn/leftTicket/init']")[0].click() #self.browser.fill("leftTicketDTO.from_station_name",self._from) #self.browser.fill("leftTicketDTO.to_station_name",self._to) self.browser.cookies.add({"_jc_save_fromStation":"%u5317%u4EAC%2CBJP"}) self.browser.cookies.add({'_jc_save_toStation':'%u90AF%u90F8%2CHDP'}) #self.browser.fill("leftTicketDTO.train_date",self.date) self.browser.cookies.add({"_jc_save_fromDate":self.date}) isload = False while not isload: self.browser.reload() isload = self.browser.is_element_present_by_id("query_ticket",wait_time=10) if not isload: time.sleep(30) continue qt = self.browser.find_by_id("query_ticket") qt.click() print self.browser.cookies.all() flag = False while not flag: try: stations = self.browser.find_by_text(u"预订") station = [] for i in range(11,21): station = stations[i] flag =station.has_class("btn72") if flag: break if not flag: qt = self.browser.find_by_id("query_ticket").click() time.sleep(2) continue station.click() time.sleep(1) except Exception as e: print e.message flag = False p1 = self.browser.find_by_xpath("//label[@for='normalPassenger_0']") p2 = self.browser.find_by_xpath("//label[@for='normalPassenger_3']") print 'p1,p2:%s %s' % (p1,p2) if p1.is_empty(): p1 = self.browser.find_by_id("normalPassenger_0")[0].click() else : p1[0].click() if p2.is_empty(): p2 = self.browser.find_by_id("normalPassenger_3")[0].click() else : p2[0].click() self.browser.evaluate_script("confirm('抢到票了帅哥,订票吗')") time.sleep(15) self.browser.find_by_id("submitOrder_id").click()
# ================================================================================================ from splinter.browser import Browser ticket = Browser(driver_name = 'chrome') ticket.visit('http://www.zhihu.com/') ## xx.fill("wd", "dataman")即可在搜索栏搜索dataman ## button = xx.find_by_value(u"提问") button = ticket.find_by_id(u"zu-top-add-questionSBSBSBSBSBSBSB") # ================================================================================================ # 12306抢票 # ================================================================================================ from splinter.browser import Browser client = Browser(driver_name= 'chrome') url = 'https://kyfw.12306.cn/otn/leftTicket/init' client.visit(url) client.find_by_text(u'登录').click() client.fill('loginUserDTO.user_name','*****@*****.**') client.fill("userDTO.password","family40440412306") #填写出发点目的地 client.cookies.add({"_jc_save_fromStation":"%u4E0A%u6D77%2CSHH"}) client.cookies.add({"_jc_save_fromDate":"2016-01-20"}) client.cookies.add({u'_jc_save_toStation':'%u6C38%u5DDE%2CAOQ'}) #加载查询 client.reload() client.find_by_text(u"查询").click() #预定 client.find_by_text(u"预订")[1].click() #选择乘客 client.find_by_text(u"数据分析侠")[1].click() #-*- coding:utf-8 -*-
def IsBot(user, follower, link): print("\nCHECKING IF {} IS A BOT".format(user)) global bot global nonbot global browsers global browser_available global antidoubler global browser global primary_count if IsKnownBot(user): print("USER {} IS A BOT".format(user)) return True elif user in nonbot or UserInDB(user): statement = ("INSERT INTO Gab.Follows(Follower, Followee)VALUES(%s,%s)") values = (user,link) if follower else (link, user) if not LinkInDB(values[0], values[1]): DBexecuter("insert", statement, values, cursor, cnx) return False else: print("DOING FIRST TIME PROCESSING FOR {}".format(user)) # check if we need to reset browser primary_count += 1 if primary_count >= BROWSE_LIMIT: browser.quit() crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browser = crawler primary_count = 0 print("PRIMARY BROWSER RESET") # check if user is bot or not page = getHTML("https://gab.ai/" + user) soup = BeautifulSoup(page.html, features="html.parser") isBot = False try: # find bio bio = soup.find('div', {'class':'profile__bio'}) # bio message bioMessage = bio.find("div").text # find metadata profileBar = soup.find('div', {'class':'profile__bar'}) meta = profileBar.findAll('a') # number of posts numposts = int(meta[1].text.split()[0].replace(',', '')) # number of followers numfollowers = int(meta[2].text.split()[0].replace(',', '')) # number of following numfollowing = int(meta[3].text.split()[0].replace(',', '')) # perform bot check logic if numposts == 0: isBot = True elif float(numfollowers)/numposts > 100: isBot = True elif numfollowing != 0 and float(numfollowing)/numposts > 100: isBot = True elif bioMessage[0] == '"' or bioMessage[0] == "'": isBot = True elif len(bioMessage.split()) >= 3: bioList = bioMessage.split() if bioList[-2][0] == '-' or bioList[-3] == '-': isBot = True except: print("ERROR PARSING USER") isBot = True # update bot / nonbot if isBot: print("USER {} IS A BOT".format(user)) AddBot(user) #bot.add(user) else: nonbot.add(user) # if not bot, parse it here and now instead of reloading it # if is bot, push name into meta db if isBot: try: print("Adding {} to meta bot DB".format(user)) statement = """insert into bots (name) values("{}")""".format(user) metacursor.execute(statement) metacon.commit() except: print("Failed to add {} to meta bot DB".format(user)) else: # that is, not isBot print("ADDING NEW USER {} TO DATABASE AND SCRAPING POSTS".format(user)) if not UserInDB(user) and not user in antidoubler: try: antidoubler.add(user) AddUser(user) print("GETTING POSTS FOR USER " + user) #print("JUST CHECKING... LEN BROWSERS is {}".format(len(browsers))) # grab posts here # check for an available browser while True: found = False for i in range(browser_limit): #print("A - iteration " + str(i)) if browser_available[i] == True: found = True #print("B - browser available") browser_available[i] = False #print("C - set browser to unavailable") #p = Process(target=scrapePosts, args=(i, user)) p = threading.Thread(target=scrapePosts, args=(i, user)) #print("D - process set") threads.append(p) #print("E - appended thread to threads list") p.start() #print("F - started thread") print(user + " IS BEING CRAWLED ON BROWSER " + str(i)) break if found: print("DUPLICATE REQUEST IGNORED") break time.sleep(1) except: print("ENCOUNTERED EXCEPTION DOWNLOADING POSTS") return isBot if UserInDB(user): statement = ("INSERT INTO Gab.Follows(Follower, Followee)VALUES(%s,%s)") values = (user,link) if follower else (link, user) if not LinkInDB(values[0], values[1]): DBexecuter("insert", statement, values, cursor, cnx) return isBot
class huoche(object): """docstring for huoche""" driver_name = '' executable_path = '' #用户名,密码 username = u"12306用户名" passwd = u"12306密码" # cookies值得自己去找, 下面两个分别是上海, 太原南 starts = u"%u4E0A%u6D77%2CSHH" ends = u"%u592A%u539F%2CTYV" # 时间格式2018-01-19 dtime = u"2018-01-19" # 车次,选择第几趟,0则从上之下依次点击 order = 0 ###乘客名 users = [u"你的名字"] ##席位 xb = u"二等座" pz = u"成人票" """ 网址 """ """ 12306查询URL """ ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init" """ 12306登录URL """ login_url = "https://kyfw.12306.cn/otn/login/init" """ 我的12306 """ initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" """ 购票确认URL """ buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc" def __init__(self): self.driver_name = 'chrome' self.executable_path = '/usr/local/bin/chromedriver' def login(self): self.driver.visit(self.login_url) # 填充密码 self.driver.fill("loginUserDTO.user_name", self.username) # sleep(1) self.driver.fill("userDTO.password", self.passwd) print u"等待验证码,自行输入..." while True: if self.driver.url != self.initmy_url: sleep(1) else: break def start(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(1400, 1000) self.login() # sleep(1) self.driver.visit(self.ticket_url) try: print u"购票页面开始..." # sleep(1) # 加载查询信息 self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() count = 0 if self.order != 0: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 print u"循环点击查询... 第 %s 次" % count # sleep(1) try: self.driver.find_by_text(u"预订")[self.order - 1].click() except Exception as e: print e print u"还没开始预订" continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 print u"循环点击查询... 第 %s 次" % count # sleep(0.8) try: for i in self.driver.find_by_text(u"预订"): i.click() sleep(1) except Exception as e: print e print u"还没开始预订 %s" % count continue print u"开始预订..." # sleep(3) # self.driver.reload() sleep(1) print u'开始选择用户...' for user in self.users: self.driver.find_by_text(user).last.click() print u"提交订单..." sleep(1) # self.driver.find_by_text(self.pz).click() # self.driver.find_by_id('').select(self.pz) # # sleep(1) # self.driver.find_by_text(self.xb).click() # sleep(1) self.driver.find_by_id('submitOrder_id').click() # print u"开始选座..." # self.driver.find_by_id('1D').last.click() # self.driver.find_by_id('1F').last.click() sleep(1.5) print u"确认选座..." self.driver.find_by_id('qr_submit_id').click() except Exception as e: print e
class huoche(object): """docstring for huoche""" driver_name='' executable_path='' #用户名,密码 username="******" passwd ="password" # cookies值得自己去找, 下面两个分别是上海, 太原南 starts = "%u5317%u4EAC%2CBJP" ends = "%u4E34%u6C82%2CLVK" # 时间格式2018-01-19 dtime = "2018-02-12" # 车次,选择第几趟,0则从上之下依次点击 order = 0 ###乘客名 users = ["XXX"] ##席位 xb = "硬座" pz="成人票" """网址""" ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init" login_url = "https://kyfw.12306.cn/otn/login/init" initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" buy="https://kyfw.12306.cn/otn/confirmPassenger/initDc" login_url='https://kyfw.12306.cn/otn/login/init' def __init__(self): self.driver_name='chrome' def login(self): self.driver.visit(self.login_url) self.driver.fill("loginUserDTO.user_name", self.username) # sleep(1) self.driver.fill("userDTO.password", self.passwd) print ("等待验证码,自行输入...") while True: if self.driver.url != self.initmy_url: sleep(1) else: break def start(self): self.driver=Browser(driver_name=self.driver_name) self.driver.driver.set_window_size(1400, 1000) self.login() # sleep(1) self.driver.visit(self.ticket_url) try: print ("购票页面开始...") # sleep(1) # 加载查询信息 self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() count=0 if self.order!=0: while self.driver.url==self.ticket_url: self.driver.find_by_text("查询").click() count += 1 print("循环点击查询... 第 %s 次" % count) # sleep(1) try: self.driver.find_by_text("预订")[self.order - 1].click() except Exception as e: print(e) print("还没开始预订") continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text("查询").click() count += 1 print("循环点击查询... 第 %s 次" % count) # sleep(0.8) try: for i in self.driver.find_by_text("预订"): i.click() sleep(1) except Exception as e: print(e) print("还没开始预订 %s" % count) continue print("开始预订...") # sleep(3) # self.driver.reload() sleep(1) print('开始选择用户...') for user in self.users: self.driver.find_by_text(user).last.click() print("提交订单...") sleep(1) # self.driver.find_by_text(self.pz).click() # self.driver.find_by_id('').select(self.pz) # # sleep(1) # self.driver.find_by_text(self.xb).click() # sleep(1) self.driver.find_by_id('submitOrder_id').click() # print u"开始选座..." # self.driver.find_by_id('1D').last.click() # self.driver.find_by_id('1F').last.click() sleep(1.5) print("确认选座...") self.driver.find_by_id('qr_submit_id').click() except Exception as e: print(e)
def huoche(): global b # 使用splinter打开chrome浏览器 b = Browser(driver_name="chrome") # 返回购票页面 b.visit(ticket_url) while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: print(u"购票页面...") # 跳回购票页面 b.visit(ticket_url) # 加载查询信息 b.cookies.add({"_jc_save_fromStation": start_sta}) b.cookies.add({"_jc_save_toStation": end_sta}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() sleep(2) count = 0 # 选择车次 # b.find_by_id(u"show_more").click(); # b.find_by_id(u"inp-train").fill(train_no); # b.find_by_id(u"add-train").click(); # b.find_by_id(u"show_more").click(); # 选择类型 b.find_by_text(train_type).click() # 循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) sleep(interval) try: b.find_by_text(u"预订")[order - 1].click() except: print(u"不能预订") continue else: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) sleep(interval) try: for i in b.find_by_text(u"预订"): i.click() except: print(u"不能预订") continue sleep(1) b.find_by_text(pa)[1].click() notify.Beep(300, 3000) notify.MessageBoxW("看票", "票来了") print(u"快输入验证码抢票啊啊 啊") except Exception as e: print(traceback.print_exc())
def scrapePosts(b_index, user): global browser_available global browsers print("BROWSER IN USE: " + str(b_index) + " OF " + str(len(browser_available))) #print("BROWSERLEN " + str(len(browsers))) #browser = browsers[b_index] #browser = Browser('firefox') #browser.visit("http://gab.ai") #browser.driver.implicitly_wait(3) #username = "******" #password = "******" #browser.find_by_name("username").fill(username) #browser.find_by_name("password").fill(password) #print(browser.find_by_text("Login")[1].click()) browser = browsers[b_index] # reset if necessary browser_count[b_index] += 1 try: if browser_count[b_index] >= BROWSE_LIMIT: browser.quit() crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browsers[b_index] = crawler browser_count[b_index] = 0 print("BROWSER {} RESET".format(b_index)) except: print("failed to reset browser") browser = browsers[b_index] browser.visit("http://gab.ai/" + user) browser.driver.implicitly_wait(3) try: soup = BeautifulSoup(browser.html, features="html.parser") except selenium.common.exceptions.TimeoutException: browser_available[b_index] = True return AttributeError i = 0 count = 0 exceptcounter = 0 try: load_count = 0 while browser.find_by_text("Load more ") and load_count <= LOAD_LIMIT: browser.driver.implicitly_wait(1) time.sleep(1) try: browser.find_by_css('.post-list__load-more').click() load_count += 1 soup = BeautifulSoup(browser.html, features="html.parser") except Exception: exceptcounter += 1 if exceptcounter == 2: break continue posts = soup.findAll("post") if len(posts) == count: break count = len(posts) except selenium.common.exceptions.WebDriverException: browser_available[b_index] = True return soup = BeautifulSoup(browser.html, features="html.parser") postlist = soup.findAll("post") for i, post in enumerate(postlist): try: ScrapAndAddPosts(post, user, b_index) except Exception as e: error = "FAILED TO PROCESS POST {} FOR USER {}: {}".format(i,user, e) print(error) errorlog.write(error) browser_available[b_index] = True
# find cookies with devtools of browsers ''' cookiedict_update = {"_jc_save_fromDate":"2016-02-01", "_jc_save_fromStation":"%u53A6%u95E8%2CXMS", "_jc_save_toStation":"%u5317%u4EAC%2CBJP", } brs = Browser(driver_name=browsername) brs.visit(site) brs.find_by_id(u'login_user').click() brs.fill('loginUserDTO.user_name', loginname) brs.fill('userDTO.password', loginpass) #sleep(10) input("Press Enter to continue...") # choose picture here ''' # jump to ticket order page ''' brs.find_by_id(u'selectYuding').click() brs.cookies.add(cookiedict_update) print(brs.cookies.all()) brs.reload() brs.find_by_id(searchtype_id).click() brs.find_by_id(u'query_ticket').click() brs.find_by_text(u"预订")[1].click() brs.find_by_id(passenger_id).click()
class BrushTicket(object): """买票类及实现方法""" def __init__(self, passengers, from_time, from_station, to_station, numbers, seat_type, receiver_mobile, receiver_email): """定义实例属性,初始化""" # 乘客姓名 self.passengers = passengers # 起始站和终点站 self.from_station = from_station self.to_station = to_station # 车次 self.numbers = list(map(lambda number: number.capitalize(), numbers)) # 乘车日期 self.from_time = from_time # 座位类型所在td位置 if seat_type == '商务座特等座': seat_type_index = 1 seat_type_value = 9 elif seat_type == '一等座': seat_type_index = 2 seat_type_value = 'M' elif seat_type == '二等座': seat_type_index = 3 seat_type_value = 0 elif seat_type == '高级软卧': seat_type_index = 4 seat_type_value = 6 elif seat_type == '软卧': seat_type_index = 5 seat_type_value = 4 elif seat_type == '动卧': seat_type_index = 6 seat_type_value = 'F' elif seat_type == '硬卧': seat_type_index = 7 seat_type_value = 3 elif seat_type == '软座': seat_type_index = 8 seat_type_value = 2 elif seat_type == '硬座': seat_type_index = 9 seat_type_value = 1 elif seat_type == '无座': seat_type_index = 10 seat_type_value = 1 elif seat_type == '其他': seat_type_index = 11 seat_type_value = 1 else: seat_type_index = 7 seat_type_value = 3 self.seat_type_index = seat_type_index self.seat_type_value = seat_type_value # 通知信息 self.receiver_mobile = receiver_mobile self.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/downloads self.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: break def start_brush(self): """买票功能实现""" # 浏览器窗口最大化 self.driver.driver.maximize_window() # 登陆 self.do_login() # 跳转到抢票页面 self.driver.visit(self.ticket_url) sleep(1) 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 = 0 while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() sleep(1) count += 1 print('第%d次点击查询……' % count) try: start_list = self.driver.find_by_css('.start-t') for start_time in start_list: current_time = start_time.text current_tr = start_time.find_by_xpath('ancestor::tr') if current_tr: car_no = current_tr.find_by_css('.number').text if car_no in self.numbers: if current_tr.find_by_tag('td')[ self.seat_type_index].text == '--': print( '%s无此座位类型出售,继续……' % (car_no + '(' + current_time + ')', )) sleep(0.2) elif current_tr.find_by_tag('td')[ self.seat_type_index].text == '无': print( '%s无票,继续尝试……' % (car_no + '(' + current_time + ')', )) sleep(0.2) else: # 有票,尝试预订 print(car_no + '(' + current_time + ')刷到票了(余票数:' + 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 = 1 for 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 += 1 sleep(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].text if 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) continue print('正在确认订单……') self.driver.find_by_id( 'qr_submit_id').click() print('预订成功,请及时前往支付……') # 发送通知信息 self.send_mail(self.receiver_email, '恭喜您,抢到票了,请及时前往12306支付订单!') self.send_sms( self.receiver_mobile, '您的验证码是:8888。请不要把验证码泄露给其他人。') sys.exit(0) else: print('当前车次异常') except Exception as error_info: print(error_info) 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_str def send_mail(self, receiver_address, content): """发送邮件通知""" # 连接邮箱服务器信息 host = 'smtp.163.com' port = 25 sender = '*****@*****.**' # 你的发件邮箱号码 pwd = 'FatBoy666' # 不是登陆密码,是客户端授权密码 # 发件信息 receiver = receiver_address body = '<h2>温馨提醒:</h2><p>' + content + '</p>' msg = MIMEText(body, 'html', _charset="utf-8") msg['subject'] = '抢票成功通知!' msg['from'] = sender msg['to'] = receiver s = smtplib.SMTP(host, port) # 开始登陆邮箱,并发送邮件 s.login(sender, pwd) s.sendmail(sender, receiver, msg.as_string())
class huoche(object): driver_name = '' executable_path = '' #用户名,密码 username = u"xxx" passwd = u"xxx" # cookies值得自己去找, 下面两个分别是沈阳, 哈尔滨 starts = u"%u6C88%u9633%2CSYT" ends = u"%u54C8%u5C14%u6EE8%2CHBB" # 时间格式2018-01-19 dtime = u"2018-01-19" # 车次,选择第几趟,0则从上之下依次点击 order = 0 ###乘客名 users = [u"xxx",u"xxx"] ##席位 xb = u"二等座" pz = u"成人票" """网址""" ticket_url = "https://kyfw.12306.cn/otn/leftTicket/init" login_url = "https://kyfw.12306.cn/otn/login/init" initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" buy = "https://kyfw.12306.cn/otn/confirmPassenger/initDc" def __init__(self): self.driver_name = 'chrome' self.executable_path = 'D:/chromedriver' def login(self): self.driver.visit(self.login_url) self.driver.fill("loginUserDTO.user_name", self.username) # sleep(1) self.driver.fill("userDTO.password", self.passwd) print(u"等待验证码,自行输入...") while True: if self.driver.url != self.initmy_url: sleep(1) else: break def start(self): self.driver = Browser(driver_name=self.driver_name,executable_path=self.executable_path) self.driver.driver.set_window_size(1400, 1000) self.login() # sleep(1) self.driver.visit(self.ticket_url) try: print(u"购票页面开始...") # sleep(1) # 加载查询信息 self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() count = 0 if self.order != 0: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) # sleep(1) try: self.driver.find_by_text(u"预订")[self.order - 1].click() except Exception as e: print(e) print(u"还没开始预订") continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text(u"查询").click() count += 1 print(u"循环点击查询... 第 %s 次" % count) # sleep(0.8) try: for i in self.driver.find_by_text(u"预订"): i.click() sleep(1) except Exception as e: print(e) print(u"还没开始预订 %s" % count) continue print(u"开始预订...") # sleep(3) # self.driver.reload() sleep(1) print(u'开始选择用户...') for user in self.users: self.driver.find_by_text(user).last.click() print(u"提交订单...") sleep(1) self.driver.find_by_text(self.pz).click() self.driver.find_by_id('').select(self.pz) # sleep(1) self.driver.find_by_text(self.xb).click() sleep(1) self.driver.find_by_id('submitOrder_id').click() print(u"开始选座...") self.driver.find_by_id('1D').last.click() self.driver.find_by_id('1F').last.click() sleep(1.5) print(u"确认选座...") self.driver.find_by_id('qr_submit_id').click() except Exception as e: print(e)
# -*- coding:utf-8 -*- from splinter.browser import Browser b = Browser(driver_name="chrome") url = "https://kyfw.12306.cn/otn/leftTicket/init" b.visit(url) b.find_by_text(u"登录").click() b.fill("loginUserDTO.user_name","qianyu668899") b.fill("userDTO.password","qsjy81zwxy") b.cookies.all() b.cookies.add({"_jc_save_fromStation":u"上海"}) b.cookies.add({"_jc_save_fromDate":"2016-02-01"}) b.cookies.add({u'_jc_save_toStation':u'南昌'}) b.cookies.all() b.reload() b.find_by_text(u"查询").click()
def huoche(): global b b = Browser(driver_name="chrome") b.visit(ticket_url) while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: print (u"购票页面") # 跳回购票页面 b.visit(ticket_url) # 加载查询信息 b.cookies.add({"_jc_save_fromStation": starts}) b.cookies.add({"_jc_save_toStation": ends}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() b.find_by_text(u"更多选项").click() sleep(1) b.find_by_id("inp-train").fill(tnum) b.find_by_id("add-train").click() #b.find_by_text(u"K-快速").click() b.find_by_text(u"请选择")[1].click() b.find_by_text(seatcn)[1].click() b.execute_script("$.closeSelectSeat()") sleep(2) count = 0 flag = "" # 循环点击预订 print ("order is %s" % order) if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() count +=1 print (u"循环点击查询... 第 %s 次" % count) sleep(5) try: b.find_by_text(u"预订")[order - 1].click() except: print (u"还没开始预订") continue else: while b.url == ticket_url: b.find_by_text(u"查询").click() count += 1 #flag = b.find_by_id("YW_240000K11711")[0].text #print ("ticket count is %s" % flag) sleep(2) #b.execute_script("$('a:contains("+tnum+")').closest('tr').children('td:eq("+seat+")').addClass('abcde')") #flag = b.find_by_xpath("//td[@class='abcde']").text flag = b.find_by_xpath("//td[contains(@id,'"+seat+"')][contains(@id,'"+tnum+"')]").text print (u"循环点击查询... 第 %s 次 tickets count is %s" % (count,flag)) try: #for i in b.find_by_text(u"预订"): # i.click() if flag != "--" and flag != u"无": b.execute_script("$('a:contains("+tnum+")').closest('tr').children('td:last').children().click()") break except: print (u"还没开始预订") continue sleep(3) pat = pa.split(",") while True: try: flag = True for p in pat: if p == usernamecn: if b.find_by_text(p)[1].checked != True: flag = False b.find_by_text(p)[1].check() flag = True else: if b.find_by_text(p)[0].checked != True: flag = False b.find_by_text(p)[0].check() flag = True print (u"选择乘客:%s" % p) if flag: break except: print (u"努力选中陛下的乘客信息中~~~") sleep(0.5) print ( u"能做的都做了.....不再对浏览器进行任何操作") except Exception as e: print(traceback.print_exc())