def round_trip(DepartCity, ReturnCity, departDate, returnDate, debug=0): #global se start_time = datetime.now() url = 'http://flights.ctrip.com/international/round-%s-%s-%s-%s?%s&%s&y_s' % ( DepartCity, ReturnCity, code(DepartCity), code(ReturnCity), departDate, returnDate) #print(url) ctrip_access = False while ctrip_access == False: se = Session(Ghost(), wait_timeout=30, wait_callback=None, display=True, viewport_size=(800, 680), download_images=False) se.delete_cookies() proxy = choice(proxypool) se.set_proxy(proxy[0], proxy[1], int(proxy[2])) try: se.open(url, user_agent=choice(ua_list)) #print('已打开 %s' % url) except: se.exit() del se proxypool.remove(proxy) print("blacklist %s" % proxy[1]) continue ctrip_access = se.exists('li:nth-child(5) > span') if ctrip_access == False: se.exit() del se proxypool.remove(proxy) print("blacklist %s" % proxy[1]) se.click('#sortControls > ul > li:nth-child(5) > span') if se.exists('i.icon-reverse') == True: se.click('#sortControls > ul > li:nth-child(5) > span') se.wait_while_selector('#FI_progBar', timeout=20) #print('Loading finished!') se.sleep(0.2) html = se.content soup = BeautifulSoup(html, "html.parser") source = soup.select('#flightList > div') if debug == 1: return source lowest = source[0].select('span.price2')[0].text end_time = datetime.now() timedelsta = (end_time - start_time).seconds print('%s-%s往返 %s去 %s回 最低价%s 搜索耗时%s秒' % (DepartCity, ReturnCity, departDate, returnDate, lowest, timedelsta)) se.exit() del se price = lowest[1:] insert_price(DepartCity, ReturnCity, departDate, returnDate, price)
ghost = Ghost() USERAGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0" with ghost.start(): session = Session(ghost, download_images=False, display=True, user_agent=USERAGENT, viewport_size=(800, 600)) page, rs = session.open("https://m.facebook.com/login.php", timeout=120) assert page.http_status == 200 session.evaluate(""" document.querySelector('input[name="email"]').value = '*****@*****.**' document.querySelector('input[name="pass"]').value = 'wikipedia150101facebook'; """) session.evaluate("""document.querySelector('input[name="login"]').click();""", expect_loading=True) """ import codecs with codecs.open('fb.html', encoding='utf-8', mode='w') as f: f.write(session.content) """ # session.save_cookies('fbookie') session.capture_to(path='fbookie.png') # gracefully clean off to avoid errors session.webview.setHtml('') session.exit()