Beispiel #1
0
def view_public_feeder_as_user():
    #opts = Options()
    #opts.set_headless()
    #assert opts.headless
    #browser = Chrome(options=opts)
    browser = Chrome()
    browser.get('http://localhost:5000')
    username_input = browser.find_elements_by_id('username')[0]
    username_input.send_keys('test')
    password_input = browser.find_elements_by_id('password')[0]
    password_input.send_keys('test')
    login_button = browser.find_elements_by_id(
        'loginForm')[0].find_elements_by_tag_name('button')[0]
    login_button.click()
    demo_batter_olin_barre_gh_battery = browser.find_elements_by_link_text(
        'Demo Batter Olin Barre GH Battery')[0]
    demo_batter_olin_barre_gh_battery.click()
    olin_barre_gh = browser.find_elements_by_id('feederButton')[1]
    olin_barre_gh.click()
    time.sleep(5)
    browser_errors = []
    for log in browser.get_log('browser'):
        if log['level'] == 'SEVERE':
            browser_errors.append(log)
    if len(browser_errors) > 0:
        raise Exception(f'Test failed: {browser_errors}')
    while True:
        pass
Beispiel #2
0
def toService(driver: webdriver.Chrome, serviceName: str):
    toCe(driver)
    driver.get("http://cloudengine.yunzong.me:10470/apply/list#allApplyInfo")
    Select(
        driver.find_elements_by_id(
            "select2-button-addons-single-input-group-sm")
        [1]).select_by_visible_text("zys-pay")
Beispiel #3
0
def get_reddit_api_keys():
    pass_chrome_binary()
    os.environ['webdriver.chrome.driver'] = './chromedriver'
    driver = Chrome('./chromedriver')

    # Log in
    driver.get('https://www.reddit.com/prefs/apps')
    driver.find_element_by_id("loginUsername").send_keys(USERNAME)
    driver.find_element_by_id("loginPassword").send_keys(PASSWORD)
    driver.find_element_by_xpath(
        "//button[@class='AnimatedForm__submitButton'][@type='submit']"
    ).submit()

    # Create dummy app
    time.sleep(4)
    driver.refresh()
    driver.find_element_by_id("create-app-button").click()
    driver.find_element_by_xpath(
        "//*[@id='create-app']/table/tbody/tr[1]/td/input").send_keys('test')
    driver.find_element_by_xpath("//*[@id='app_type_script']").click()
    driver.find_element_by_xpath(
        "//*[@id='create-app']/table/tbody/tr[5]/td/textarea").send_keys(
            'for DankMemeBot')
    driver.find_element_by_xpath(
        "//*[@id='create-app']/table/tbody/tr[7]/td/input").send_keys(
            'http://*****:*****@id='create-app']/button").submit()

    # Regex Oath2 access token and secret key
    app_details = driver.find_elements_by_id("developed-apps")[-1].text
    access_token = re.findall('personal use script\\n(.+)\\n', app_details)[-1]
    secret = re.findall('\\nsecret(.+)\\n', app_details)[-1]

    return access_token, secret
Beispiel #4
0
def toIdc(driver: webdriver.Chrome):
    driver.execute_script("window.open('http://idcenter.box.zonghengke.com/')")
    driver.switch_to.window(driver.window_handles[-1])
    if (len(driver.find_elements_by_id("in_user_Nm")) > 0):
        driver.find_element_by_id("in_user_Nm").send_keys("gaowenbo")
        driver.find_element_by_id("in_password").send_keys("YKUacrVjlfoR")
        driver.find_element_by_id("sign_in").click()
Beispiel #5
0
    def __init__(self, driver: webdriver.Chrome):

        print("")

        # Find opening hours list by id
        openingHours = driver.find_elements_by_id("openingHours")
        # For loop that loops through all the items in the list and prints them
        for item in openingHours:
            print(item.text)
Beispiel #6
0
def _get_logged_in_browser():
    browser = Chrome()
    browser.get('http://localhost:5000')
    username_input = browser.find_elements_by_id('username')[0]
    username_input.send_keys('test')
    password_input = browser.find_elements_by_id('password')[0]
    password_input.send_keys('test')
    login_button = browser.find_elements_by_id(
        'loginForm')[0].find_elements_by_tag_name('button')[0]
    login_button.click()
    demo_batter_olin_barre_gh_battery = browser.find_elements_by_link_text(
        'Demo Batter Olin Barre GH Battery')[0]
    demo_batter_olin_barre_gh_battery.click()
    olin_barre_gh = browser.find_elements_by_id('feederButton')[1]
    olin_barre_gh.click(
    )  # This opens a new tab, but selenium is still in the original tab
    time.sleep(5)
    return browser
Beispiel #7
0
def enter_game(driver: webdriver.Chrome,
               room_name: str,
               room: int,
               password: str = None) -> None:
    """ Enters a game from the home page. """
    driver.find_element_by_id("mpPlayButton").click()

    driver.find_element_by_id("rbSearchInput").send_keys(room_name)
    time.sleep(1)
    top = driver.find_element_by_id(f"rbRoom-{room}")
    top.find_element_by_class_name("rbrJoinButton").click()

    title = driver.find_elements_by_id("swal2-title")
    if len(title) > 0:
        driver.find_element_by_class_name("swal2-input").send_keys(password)
        driver.find_element_by_class_name("swal2-confirm").click()

    time.sleep(1)
    ready_up(driver)
Beispiel #8
0
		url = "https://www.youtube.com/results?search_query="+sub_cat1+"+"+place_name+"+india"+"&sp=EgIQAg%253D%253D"
		driver.get(url)
		
		SCROLL_PAUSE_TIME = 5
		last_height = driver.execute_script("return document.documentElement.scrollHeight")
		while True:
			driver.execute_script("window.scrollTo(0,document.documentElement.scrollHeight);")
			time.sleep(SCROLL_PAUSE_TIME)
			new_height = driver.execute_script("return document.documentElement.scrollHeight")
			print(new_height)
			if (new_height == last_height):
				print("break")
				break
			last_height = new_height

		results = driver.find_elements_by_id("content-section")
		for result in results:
			name=None
			try:
				name = result.find_element_by_id('text').text
			except:
				name=None
			finally:
				subscribers=None
				try:
					subscribers = result.find_element_by_id('subscribers').text
				except:
					subscribers=None
				finally:
					videos=None
					try:
def iex_Rtm_import():
    # set download directory path
    p = {
        'download.default_directory':
        r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\iexRtmFile'
    }
    #add options to browser
    opts.add_experimental_option('prefs', p)

    browser = Chrome(options=opts)
    #maximize browser
    browser.maximize_window()
    # open the website "https://www.iexindia.com/marketdata/rtm_market_snapshot.aspx"
    browser.get('https://www.iexindia.com/marketdata/rtm_market_snapshot.aspx')
    # TEST WAIT UNTIL IN SELENIUM
    delay = 120  # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located(
                (By.ID, 'ctl00_InnerContent_ddlPeriod')))
    except TimeoutException:
        print("Loading took too much time!")
    # open the dropdown select button "ctl00_InnerContent_ddlPeriod" to select day
    iexDrpdwnDam = Select(
        browser.find_elements_by_id("ctl00_InnerContent_ddlPeriod")[0])

    # select day element of dropdown by index # "yesterday"
    iexDrpdwnDam.select_by_index(0)

    # click on update report buttton "ctl00_InnerContent_btnUpdateReport"
    browser.find_elements_by_id(
        "ctl00_InnerContent_btnUpdateReport")[0].click()
    # updateReport.click()
    # wait until dropdown is opened
    dropdownLinks = []
    delay = 60  # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located(
                (By.ID,
                 'ctl00_InnerContent_reportViewer_ctl05_ctl04_ctl00_ButtonImg'
                 )))
    except TimeoutException:
        print("Loading took too much time!")
    dropdownLinks = browser.find_elements_by_id(
        "ctl00_InnerContent_reportViewer_ctl05_ctl04_ctl00_ButtonImg")

    # click on different download option button "ctl00_InnerContent_reportViewer_ctl05_ctl04_ctl00_ButtonLink"
    dropdownLinks[0].click()

    # move all iex files from "C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\iexDamFile" to
    #                           "C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\iexDamFile\Archives"
    srcFileLocation = r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\iexRtmFile'
    destFileLocation = r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\iexRtmFile\Archives'
    destFileName = "MarketMinute_"
    moveFilesToArchive(srcFileLocation, destFileLocation, destFileName)

    excelLinks = []
    delay = 60  # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located((By.LINK_TEXT, 'Excel')))
    except TimeoutException:
        print("Loading took too much time!")
    excelLinks = browser.find_elements(By.LINK_TEXT, "Excel")

    excelLinks[0].click()

    print("iex rtm fetch succesful")

    time.sleep(15)
    browser.close()
Beispiel #10
0
class Browser():
    '''
        使用selenium调用无头Chrome,获取需要执行js才能获取的RAIL_EXPIRATION和RAIL_DEVICEID。
        >>> browser = Browser()
        >>> browser.init()
        >>> browser.ticket_check('北京', '石家庄')  # 站名是随意的
        >>> cookie = browser.get_cookie()  # 返回的是一个包含cookie内容的python字典
    '''

    url_init = 'https://kyfw.12306.cn/otn/leftTicket/init'

    def __init__(self, flag='prod'):
        if flag == 'dev':  # 此时有头,方便调试
            opt = None
        elif flag == 'prod':
            opt = ChromeOptions()
            opt.set_headless()
        self.browser = Chrome(chrome_options=opt)

    def init(self, url=url_init):
        self.browser.get(url)

    def ticket_check(self,
                     from_station,
                     to_station,
                     t=5):  # 模拟点击查票,这时自动访问logdevice
        '''
            params:
            :from_station:str, 随意一个始发站;
            :to_station:str, 随意一个到达站;
            :t:int, 延时,默认5秒。
        '''

        ts = 0
        while True:
            element_fs = self.browser.find_elements_by_id('fromStationText')
            element_ts = self.browser.find_elements_by_id('toStationText')
            element_ck = self.browser.find_elements_by_id('query_ticket')

            if element_fs and element_ts and element_ck:
                element_fs = element_fs[0]
                element_ts = element_ts[0]
                element_ck = element_ck[0]
                break

            ts += 1
            if ts == t:
                raise TimeoutError('网络貌似有点问题...T_T\n也可能是element id改变了。。。')
            time.sleep(1)

        element_fs.clear()
        element_fs.send_keys(from_station)
        element_fs.send_keys(Keys.ENTER)
        element_ts.clear()
        element_ts.send_keys(to_station)
        element_ts.send_keys(Keys.ENTER)
        element_ck.click()

    def get_cookie(self, t=5):  # 等待加载完成并返回cookie
        '''t为等待时间近似为秒'''

        ts = 0
        cookie = {}
        while True:
            cookies = self.browser.get_cookies()
            if cookies:
                for i in cookies:
                    if i['name'] not in cookie:
                        cookie[i['name']] = i['value']
                self.browser.quit()
                return cookie

            ts += 1
            if ts == t:
                raise TimeoutError('网络是不是出了问题?T_T或者返回的网页没有需要的cookie。')
            time.sleep(1)
Beispiel #11
0
def wbes_Px_iex_import():
    # set download directory path
    p = {'download.default_directory':r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\wbesPxIexFile'}
    #add options to browser
    opts.add_experimental_option('prefs', p)
    # click on the datepicker select button 
    previousDate = dt.datetime.today() - dt.timedelta(days=1)
    previousDateFormatted = previousDate.strftime('%d-%m-%Y') # format the date to dd-mm-yyyy
    month = previousDate.strftime('%B')
    prevDate = previousDate.strftime('%d')
    prevDate = int(prevDate)
    prevDate = str(prevDate)
    browser = Chrome(options=opts)
    #maximize browser
    browser.maximize_window()
    wbesRtmIexUrl = 'https://wbes.wrldc.in/Report/PXIndex#date={}|revisionno=null|type=5'.format(previousDateFormatted)
    browser.get(wbesRtmIexUrl)
    # TEST WAIT UNTIL IN SELENIUM
    delay = 60 # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'newEffectiveDate')))
    except TimeoutException:
        print("Loading took too much time!")
    # open the calender menu
    browser.find_elements_by_xpath("//*[@id=\"newEffectiveDate\"]")[0].click()
    # get the month year from website
    while(True):
        t = browser.find_elements_by_xpath("//*[@id=\"ui-datepicker-div\"]/div/div/span[1]")
        monthString = t[0].text
        if month == monthString:
            break
        else:
            browser.find_elements_by_xpath("//*[@id=\"ui-datepicker-div\"]/div/a[1]")[0].click()

    dateString = browser.find_elements_by_xpath("//*[@id=\"ui-datepicker-div\"]/table/tbody/tr/td/a")
    for t in dateString:
        if prevDate == t.text:
            t.click()
            break
    delay = 60 # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'dwnld')))
    except TimeoutException:
        print("Loading took too much time!")

    # click on different download option dwnld
    browser.find_elements_by_id("dwnld")[0].click()

    srcFileLocation = r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\wbesPxIexFile'
    destFileLocation = r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\wbesPxIexFile\Archives'
    revNum = getMaxRTMRevForDate(previousDateFormatted,13)
    destFileName = "Report-PX_IEX-("+revNum+")-"
    moveFilesToArchive(srcFileLocation, destFileLocation, destFileName)
    delay = 60 # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.LINK_TEXT, 'Excel')))
    except TimeoutException:
        print("Loading took too much time!")
    browser.find_elements(By.LINK_TEXT,"Excel")[0].click()
    print("wbes PX IEX fetch succesful")

    time.sleep(10)
    browser.close()
    os.chdir(srcFileLocation)
    for filename in os.listdir(os.getcwd()):
        if filename.endswith(".xlsx"):
            y=filename
            x=filename.replace('('+revNum+')','(0)')
            os.rename(os.path.join(srcFileLocation, y), os.path.join(srcFileLocation, x))
Beispiel #12
0
import requests
from bs4 import BeautifulSoup
from time import sleep
import csv
from selenium import webdriver
from selenium.webdriver import Chrome

url = "https://www.youtube.com/user/whatsg/videos"
chromedriver = '/Users/mclaren/Downloads/CodingFor/chromedriver'
driver = Chrome(chromedriver)
driver.get(url)
title = driver.find_elements_by_id('video-title')

titles = [i.text for i in title]
print(titles)

# print(bsobj.findAll("a",{"class":"yt-simple-endpoint style-scope ytd-grid-video-renderer"}))

# headers = {'User-Agent': 'Mozilla/5.0'}
# page = requests.get(url,headers=headers)
# bsobj = BeautifulSoup(page.content, "html.parser")
# titles = bsobj.findAll()

# print(bsobj)
Beispiel #13
0
class AmazonBot():
    def __init__(self, email, password, link=AMAZON_GIVEAWAY_LINK):
        opts = Options()
        # comment out below if you want to see the browser UI 
        #opts.set_headless()
        self.browser = Chrome(options=opts)
        self.actions = ActionChains(self.browser)
        self.email = email
        self.password = password

        # site for amazon giveaway
        self.browser.get(AMAZON_GIVEAWAY_LINK)
        print("run bot.prime_giveaway()")    

    def prime_giveaway(self):
        self.login()
        self.enter_prime_giveaways()

    def giveaway(self):
        self.login()
        self.enter_giveaways()

    def login(self):
        self.actions = ActionChains(self.browser)
        self.browser.find_elements_by_id('nav-link-accountList')[0].click()
        self.actions.send_keys(self.email).perform()
        self.browser.find_element_by_id('continue').click() 
        self.browser.find_element_by_id('ap_password').send_keys(self.password) 
        self.browser.find_element_by_id('signInSubmit').click() 

    def enter_giveaways(self):
        # get all giveaway items on page

        
        
    def enter_prime_giveaways(self):
        # get all giveaway items
        items = self.browser.find_elements_by_class_name('bxc-grid-overlay__link')

        # create tabs for each giveaway item
        actions = self.actions.key_down(Keys.COMMAND)
        for item in items:
            actions = actions.click(item)

        actions = actions.key_up(Keys.COMMAND)
        actions.perform()

        # get all tab handles
        windows = self.browser.window_handles

        # play videos in all windows
        for window in windows:
            self.browser.switch_to.window(window)
            try:
                WebDriverWait(self.browser, 1).until(lambda x: x.find_elements_by_class_name('airy-play-toggle-hint'))
                self.browser.find_elements_by_class_name('airy-play-toggle-hint')[0].click()
            except:
                print('this page doesn\'t have a video')
                self.browser.close()

        # wait 32 seconds
        time.sleep(32)

        #get new windows
        windows = self.browser.window_handles

        # click on submit giveaway bid 
        for window in windows:
            self.browser.switch_to.window(window)
            try:
                self.browser.find_element_by_id('videoSubmitForm').click()
                time.sleep(3)
                # delete this tab
                # self.browser.close()
            except:
                print('no bid found here')
Beispiel #14
0
class abc:
    global propAbhi
    global logger
    global driver

    def __init__(self):
        self.propAbhi = Properties()
        try:
            propertiesFile = open("./Files/Config.properties")
            self.propAbhi.load(propertiesFile)
            self.propAbhi.list()        
        except FileNotFoundError as e:
            print(e)

        self.logger = logging.getLogger()
        self.logger.setLevel(logging.INFO)
        self.driver = null
     
    def GetElement(self, locatorKey):
        e = null
        a = self.propAbhi[locatorKey]

        try:
            if(locatorKey.endswith("_id")):
                e = self.driver.find_element_by_id(a)
            
            elif(locatorKey.endswith("_name")):
                e = self.driver.find_element_by_name(a)
            
            elif(locatorKey.endswith("_xpath")):
                e = self.driver.find_element_by_xpath(a)
                
            else:
                self.ReportFail("Locator not correct")
                assert False ("Locator not correct")
            
            return e
        except Exception as ex:
            self.ReportFail(self, ex.message)
            print(ex)
            assert False
           
    def OpenBrowser(self, browserName):                                                                                                                                 

            print(self.propAbhi["email_xpath"])
            if(browserName == "Chrome"):
                path = "../driver/chromedriver"
                self.driver = Chrome(executable_path=path)

            elif (browserName == "Mozilla"):
                path = "../driver/geckodriver"
                self.driver = Chrome(executable_path=path)
                self.driver.implicitly_wait(5)
                self.driver.maximize_window()
             
    def Navigate(self, StringURL):
        a = self.propAbhi[StringURL]
        print(a)
        self.driver.get(a)
        pass
     
    def Type(self, StringXpath, StringData):
        a = self.propAbhi[StringXpath]
        print(a)
        b = self.propAbhi[StringData]
        print(b)
        self.driver.find_element_by_xpath(a).send_keys(b)
        pass
    
    def Click(self, locatorKey):
        self.GetElement(locatorKey).click()
        pass
            
    def verify_title(self):
        return False
    
    def isElementPresent(self, locatorKey):
        self.list = null
        a = self.propAbhi[locatorKey]
        if(locatorKey.endswith("_id")):
            self.list = self.driver.find_elements_by_id(a)
            
        elif(locatorKey.endswith("_name")):
            self.list = self.driver.find_elements_by_name(a)
            
        elif(locatorKey.endswith("_xpath")):
            self.list = self.driver.find_elements_by_xpath(a)


        else:
            self.ReportFail("Locator not correct")
            assert False ("Locator not correct")
        
        if (len(self.list) == 0):   
            return False
        else:
            return True
    
    def verifyText(self, locatorKey, expectedText):
        actualText = self.GetElement(locatorKey).text
        expectedText = self.propAbhi[expectedText]
        if(actualText == expectedText):
            return True
        else:
            return False
    
    def ReportPass(self, Stringmsg):
        
        pass
    
    def ReportFail(self, Stringmsg):
        allure.attach(self.driver.get_screenshot_as_png(), "Screenshot of the screen" , AttachmentType.PNG)
        self.logger.error("Loading Element or element not found")
        assert True 
        
        
    def TakeScreenshot(self, Stringmsg):
        pass
Beispiel #15
0
class Action:
    def __init__(self):
        self.cmoney_url = "https://www.cmoney.tw/member/login/?url=https%3a%2f%2fwww.cmoney.tw%2fvt%2faccount-manage.aspx"
        self.driver = None
        self.account_boxes = {}

    def start_browser(self, user_id, user_pw):
        # 輸入帳密
        self.driver = Chrome("./chromedriver.exe")
        # self.driver = Firefox(executable_path="./geckodriver")
        self.driver.get(self.cmoney_url)
        self.login(id_, pw_)

    def login(self, user_id, user_pw):
        self.driver.find_element_by_id("account").clear()
        self.driver.find_element_by_id("account").send_keys(user_id)
        self.driver.find_element_by_id("pw").send_keys(user_pw)
        self.driver.find_element_by_id("ContentPlaceHolder1_loginBtn").click()
        time.sleep(0.2)
        if self.driver.current_url == self.cmoney_url:
            return False
        return True

    def login_ok(self):
        account_boxes = self.driver.find_elements_by_css_selector(".mabox")
        try:
            for account_title in account_boxes:
                account_title.find_element_by_css_selector("a").get_attribute(
                    "title")

        except:
            add_account = str.lower(input("是否創建帳號?\n如果是請按y\n否請按n:"))
            try:
                if add_account == "y":
                    self.create_account()
                elif add_account == "n":
                    self.select_account()
            except:
                print("請輸入指示之文字")

        self.select_account()

    def create_account(self):
        # 新增帳號半成品

        # 第一次登入
        try:
            self.driver.find_element_by_name(
                "ctl00$ContentPlaceHolder1$ctl01").click()
            self.driver.find_element_by_id("bindFacebook").click()
            self.driver.find_element_by_class_name("current-role").click()
            self.driver.find_element_by_id("submitBtn").click()
        except:
            pass

        try:
            try:
                self.driver.find_element_by_id("addAccountLeftMenu").click()
                self.select_account()
            except:
                pass

        except:

            print("f**k")

            self.create_account()

    def select_account(self):
        self.driver.find_element_by_css_selector("a[class='v2']").click()
        account_boxes = self.driver.find_elements_by_css_selector(".mabox")
        # print(account_boxes)
        for i, m in enumerate(account_boxes):
            self.account_boxes[str(i)] = account_boxes[
                i].find_element_by_css_selector("a").get_attribute("title")

        while True:
            for i, m in enumerate(self.account_boxes):
                print('您的帳戶:', i, self.account_boxes[m])
            try:
                account_number = int(input("選擇以上帳戶:"))
                break
            except:
                print("請輸入數字:")
        try:
            path = "//*[text()[normalize-space(.) = '{}']]"
            self.driver.find_element_by_xpath(
                path.format(
                    account_boxes[account_number].find_element_by_css_selector(
                        "a").get_attribute("title"))).click()

            # 幹掉彈跳視窗
            self.close_pop_up_window()
        except:
            if account_number > len(account_boxes):
                print("沒那麼多帳號")
            else:
                print("帳戶有誤")
            self.select_account()

    def close_pop_up_window(self):
        # 關掉他媽的彈跳視窗
        print("處理中請稍後.....")
        time.sleep(4)
        try:
            # driver.find_element_by_css_selector("a.fancybox-item.fancybox-close").click()
            self.driver.refresh()
        except:
            pass
        self.trade_stock_system()

    def trade_stock_system(self):
        time.sleep(2.5)
        account_table = self.driver.find_element_by_id("AccountTabs")

        # 帳戶概覽
        def view_account():

            account_table.find_element_by_css_selector(
                "a[title='帳戶概覽']").click()

            # account_table.find_element_by_css_selector("a[href='#AI']").click()
            time.sleep(0.5)
            # 顯示帳戶股票配置

            big_table = self.driver.find_element_by_css_selector(".tabCon1")
            small_table = big_table.find_element_by_css_selector(".chart")
            stock_data = small_table.get_attribute("chartdata")
            clean_stock_data = stock_data. \
                replace('{"data":[', ''). \
                replace('"', ''). \
                replace(']}', ''). \
                replace('[', ''). \
                replace(']', ''). \
                split(',')
            pre_table = ["股票名稱or現金資產", '市值', '報酬率/%', '名稱']
            pre = prettytable.PrettyTable(pre_table, encoding='utf-8')

            for n in range(0, len(clean_stock_data), 4):
                pre.add_row(clean_stock_data[n:n + 4])
            print(pre)

            # 顯示帳戶資產配置
            asset_deploy_table = self.driver.find_element_by_css_selector(
                ".tabCon1.alldata")
            asset_deploy_tr = asset_deploy_table.find_elements_by_tag_name(
                'tr')

            pre = prettytable.PrettyTable()
            asset_deploy = []
            price = []

            for row in asset_deploy_tr:

                asset_deploy_th = row.find_elements_by_tag_name('th')
                asset_deploy_td = row.find_elements_by_tag_name('td')

                for col1 in asset_deploy_th:
                    asset_deploy += col1.text.split('ú')

                for col2 in asset_deploy_td:
                    price += col2.text.split('ú')

            pre.add_column("資產配置", asset_deploy)
            pre.add_column("價格", price)
            print(pre)

        # 委託查詢
        def order_search():
            account_table.find_element_by_css_selector(
                "a[title='委託查詢']").click()
            time.sleep(0.5)
            order_grid_title = []
            order_grid = []
            order_grid2 = []
            small_table = self.driver.find_element_by_css_selector(
                ".detailframe.recTb1Div")

            small_table_tr = small_table.find_elements_by_tag_name("tr")
            for row1 in small_table_tr:
                small_table_th = row1.find_elements_by_tag_name("th")
                for col1 in small_table_th:
                    c1 = col1.text.replace('\n(名稱)',
                                           '(名稱)').replace('交易\n類別',
                                                           '交易&類別').split(" ")
                    order_grid_title += c1

                break

            small_table2 = self.driver.find_elements_by_id(
                'EntrustQueryListInfo')
            for row2 in small_table2:
                small_table2 = row2.find_elements_by_tag_name("td")
                for col2 in small_table2:
                    c2 = col2.text.replace('\n', ' ').split('ú')
                    order_grid += c2

                break

            big_table = self.driver.find_element_by_css_selector(
                ".detailframe.recTb2Div")
            big_table_tr = big_table.find_elements_by_tag_name("tr")

            for row3 in big_table_tr:
                big_table_th = row3.find_elements_by_tag_name("th")
                for col3 in big_table_th:
                    c3 = col3.text. \
                        replace(' / ', '/'). \
                        replace('買入成本\n預估賣出收入', '買入成本&預估賣出收入') \
                        .split(' ')
                    order_grid_title += c3

                break

            big_table2 = self.driver.find_elements_by_id(
                'EntrustQueryListData')
            for row4 in big_table2:
                big_table2_td = row4.find_elements_by_tag_name("td")
                for col4 in big_table2_td:
                    c4 = col4.text. \
                        replace('/ ', '/').replace('\n', ' ').split('ú')

                    order_grid2 += c4

                break

            # print(order_grid_title)
            # print(order_grid)
            new_order_grid = []
            new_order_grid2 = []
            order_table = []

            for order_grid_len in range(0, len(order_grid), 2):
                new_order_grid.append(
                    order_grid[order_grid_len:order_grid_len + 2])

            for order_grid2_len in range(0, len(order_grid2), 6):
                new_order_grid2.append(
                    order_grid2[order_grid2_len:order_grid2_len + 6])

            for i in range(len(new_order_grid)):
                order_table.append(new_order_grid[i] + new_order_grid2[i])

            pre = prettytable.PrettyTable(order_grid_title, encoding='utf-8')

            for i in range(len(order_table)):
                pre.add_row(order_table[i])
            print(pre)

        # 交易紀錄
        def trade_record():
            account_table.find_element_by_css_selector(
                "a[title='交易紀錄']").click()
            # 下拉選單
            self.driver.find_element_by_css_selector(
                "i[class='fa fa-chevron-down']").click()
            self.driver.find_element_by_css_selector(
                "a[filtervalue='Season']").click()
            self.driver.find_element_by_id("submitfilter").click()
            record_grid_title = []
            record_grid = []
            record_grid2 = []

            small_table = self.driver.find_element_by_css_selector(
                ".detailframe.recTb1Div")

            small_table_tr = small_table.find_elements_by_tag_name("tr")

            for row1 in small_table_tr:
                small_table_th = row1.find_elements_by_tag_name("th")
                for col1 in small_table_th:
                    c1 = col1.text.replace('\n(名稱)', '(名稱)') \
                        .replace('\n', '&') \
                        .split("ú")
                    record_grid_title += c1

                break

            small_table2 = self.driver.find_elements_by_id(
                'OrderRecordListInfo')
            for row2 in small_table2:

                small_table2 = row2.find_elements_by_tag_name("td")
                for col2 in small_table2:
                    c2 = col2.text.replace('\n', ' ').split('ú')
                    record_grid += c2
                break

            big_table = self.driver.find_element_by_css_selector(
                ".detailframe.recTb2Div")
            big_table_tr = big_table.find_elements_by_tag_name("tr")

            for row3 in big_table_tr:

                big_table_th = row3.find_elements_by_tag_name("th")

                for col3 in big_table_th:
                    c3 = col3.text.replace('\n((名稱)', '(名稱)'). \
                        replace('交易\n類別', '交易&類別'). \
                        replace('成交價\n(元)', '成交價(元)'). \
                        replace('手續費\n(張)', '手續費(張)'). \
                        replace('證交稅\n(元)', '證交稅(元)'). \
                        replace('借券費\n(元)', '借券費(元)') \
                        .split('ú')

                    record_grid_title += c3

                break
            big_table2 = self.driver.find_elements_by_id('OrderRecordListData')
            for row4 in big_table2:
                big_table2_td = row4.find_elements_by_tag_name("td")
                for col4 in big_table2_td:
                    c4 = col4.text.replace('\n', ' ').split('ú')

                    record_grid2 += c4

                break
            new_record_grid = []
            new_record_grid2 = []
            record_table = []

            for order_grid_len in range(0, len(record_grid), 2):
                new_record_grid.append(
                    record_grid[order_grid_len:order_grid_len + 2])

            for order_grid2_len in range(0, len(record_grid2), 7):
                new_record_grid2.append(
                    record_grid2[order_grid2_len:order_grid2_len + 7])

            for i in range(len(new_record_grid)):
                record_table.append(new_record_grid[i] + new_record_grid2[i])

            pre = prettytable.PrettyTable(record_grid_title, encoding='utf-8')

            for i in range(len(record_table)):
                pre.add_row(record_table[i])
            print(pre)

        # 庫存明細
        def stock_detail():
            account_table.find_element_by_css_selector(
                "a[title='庫存明細']").click()

            time.sleep(0.5)
            detail_grid_title = []
            detail_grid = []
            detail_grid2 = []
            small_table = self.driver.find_element_by_css_selector(
                ".detailframe.recTb1Div")

            small_table_tr = small_table.find_elements_by_tag_name("tr")
            for row1 in small_table_tr:
                small_table_th = row1.find_elements_by_tag_name("th")
                for col1 in small_table_th:
                    c1 = col1.text.replace('\n(名稱)', '(名稱)').split(" ")
                    detail_grid_title += c1

                break

            small_table2 = self.driver.find_elements_by_id(
                'InventoryDetailListInfo')
            for row2 in small_table2:
                small_table2 = row2.find_elements_by_tag_name("td")
                for col2 in small_table2:
                    c2 = col2.text.replace('\n', ' ').split('ú')
                    detail_grid += c2

                break

            big_table = self.driver.find_element_by_css_selector(
                ".detailframe.recTb2Div")
            big_table_tr = big_table.find_elements_by_tag_name("tr")

            for row3 in big_table_tr:
                big_table_th = row3.find_elements_by_tag_name("th")
                for col3 in big_table_th:
                    c3 = col3.text.replace('\n(可平倉)', '(可平倉)'). \
                        replace(' / ', '/'). \
                        replace('買入成本\n預估賣出收入', '買入成本&預估賣出收入') \
                        .split(' ')
                    detail_grid_title += c3

                break

            big_table2 = self.driver.find_elements_by_id(
                'InventoryDetailListData')
            for row4 in big_table2:
                big_table2_td = row4.find_elements_by_tag_name("td")
                for col4 in big_table2_td:
                    c4 = col4.text. \
                        replace('/ ', '/').replace('\n', ' ').split('ú')

                    detail_grid2 += c4

                break

            new_detail_grid = []
            new_detail_grid2 = []
            detail_table = []

            for order_grid_len in range(0, len(detail_grid), 2):
                new_detail_grid.append(
                    detail_grid[order_grid_len:order_grid_len + 2])

            for order_grid2_len in range(0, len(detail_grid2), 7):
                new_detail_grid2.append(
                    detail_grid2[order_grid2_len:order_grid2_len + 7])

            for i in range(len(new_detail_grid)):
                detail_table.append(new_detail_grid[i] + new_detail_grid2[i])

            pre = prettytable.PrettyTable(detail_grid_title, encoding='utf-8')

            for i in range(len(detail_table)):
                pre.add_row(detail_table[i])
            print(pre)

        # 損益試算
        def loss_profit():
            account_table.find_element_by_css_selector(
                "a[title='損益試算']").click()
            try:
                select_loss_profit = int(input("未實現損益:選擇0\n實現損益:選擇1:"))
            except:
                print("請輸入數字")
                function_category()
            try:
                if select_loss_profit == 0:
                    fund_grid_title = []
                    fund_grid = []
                    fund_grid2 = []
                    small_table = self.driver.find_element_by_id("Div1")
                    small_table_tr = small_table.find_elements_by_tag_name(
                        "tr")
                    for row1 in small_table_tr:
                        small_table_th = row1.find_elements_by_tag_name("th")
                        for col1 in small_table_th:
                            c1 = col1.text.replace('\n(名稱)', '(名稱)').split(" ")
                            fund_grid_title += c1

                        break

                    small_table2 = self.driver.find_elements_by_id(
                        'UnaccomplishedProfitLossListInfo')
                    for row2 in small_table2:
                        small_table2 = row2.find_elements_by_tag_name("td")
                        for col2 in small_table2:
                            c2 = col2.text.replace('\n', '').split('ú')
                            fund_grid += c2

                        break

                    big_table = self.driver.find_element_by_id("Div2")
                    big_table_tr = big_table.find_elements_by_tag_name("tr")

                    for row3 in big_table_tr:
                        big_table_th = row3.find_elements_by_tag_name("th")
                        for col3 in big_table_th:
                            c3 = col3.text.replace('\n(可平倉)', '(可平倉)'). \
                                replace('買賣均價\n現價', '買賣均價&現價'). \
                                replace('買入成本\n預估賣出收入', '買入成本&預估賣出收入') \
                                .split(' ')
                            fund_grid_title += c3

                        break

                    big_table2 = self.driver.find_elements_by_id(
                        'UnaccomplishedProfitLossListData')
                    for row4 in big_table2:
                        big_table2_td = row4.find_elements_by_tag_name("td")
                        for col4 in big_table2_td:
                            c4 = col4.text.replace('\n',
                                                   '&').replace('\n',
                                                                '').split('ú')

                            fund_grid2 += c4

                        break

                    new_fund_grid = []
                    new_fund_grid2 = []
                    detail_fund_table = []

                    for fund_grid_len in range(0, len(fund_grid), 2):
                        new_fund_grid.append(
                            fund_grid[fund_grid_len:fund_grid_len + 2])

                    for fund_grid2_len in range(0, len(fund_grid2), 7):
                        new_fund_grid2.append(
                            fund_grid2[fund_grid2_len:fund_grid2_len + 7])

                    for i in range(len(new_fund_grid)):
                        detail_fund_table.append(new_fund_grid[i] +
                                                 new_fund_grid2[i])

                    pre = prettytable.PrettyTable(fund_grid_title,
                                                  encoding='utf-8')

                    for i in range(len(detail_fund_table)):
                        pre.add_row(detail_fund_table[i])
                    print(pre)

                elif select_loss_profit == 1:
                    # 下拉選單
                    self.driver.find_element_by_css_selector(
                        "a[filtervalue='accomplished']").click()
                    self.driver.find_element_by_css_selector(
                        "i[class='fa fa-chevron-down']").click()
                    self.driver.find_element_by_css_selector(
                        "a[filtervalue='Season']").click()
                    time.sleep(0.5)
                    small_table = self.driver.find_element_by_id(
                        "detailframeareaInfo")

                    big_table = self.driver.find_element_by_id(
                        "detailframeareaData")

                    fund_grid_title = []
                    fund_grid = []
                    fund_grid2 = []

                    big_table_tr = big_table.find_elements_by_tag_name("tr")
                    small_table_tr = small_table.find_elements_by_tag_name(
                        "tr")
                    for row1 in small_table_tr:
                        small_table_th = row1.find_elements_by_tag_name("th")
                        small_table_td = row1.find_elements_by_tag_name("td")
                        for col1 in small_table_th:
                            c1 = col1.text.replace('\n(名稱)', '(名稱)').split(" ")
                            fund_grid_title += c1

                        for col2 in small_table_td:
                            c2 = col2.text.replace('\n', '').split('ú')
                            fund_grid += c2

                    for row2 in big_table_tr:
                        big_table_th = row2.find_elements_by_tag_name("th")
                        big_table2_td = row2.find_elements_by_tag_name("td")
                        for col3 in big_table_th:
                            c3 = col3.text.replace('\n(可平倉)', '(可平倉)').replace(
                                '買入成本\n賣出收入', '買入成本&賣出收入').split(' ')
                            fund_grid_title += c3
                        for col5 in big_table2_td:
                            c5 = col5.text.replace('\n', '').split('ú')
                            fund_grid2 += c5

                    new_fund_grid = []
                    new_fund_grid2 = []
                    detail_fund_table = []

                    for fund_grid_len in range(0, len(fund_grid), 2):
                        new_fund_grid.append(
                            fund_grid[fund_grid_len:fund_grid_len + 2])

                    for fund_grid2_len in range(0, len(fund_grid2), 6):
                        new_fund_grid2.append(
                            fund_grid2[fund_grid2_len:fund_grid2_len + 6])

                    for i in range(len(new_fund_grid)):
                        detail_fund_table.append(new_fund_grid[i] +
                                                 new_fund_grid2[i])

                    pre = prettytable.PrettyTable(fund_grid_title,
                                                  encoding='utf-8')

                    for i in range(len(detail_fund_table)):
                        pre.add_row(detail_fund_table[i])
                    print(pre)

            except:
                if select_loss_profit > 1:
                    print("沒這個選項")
                    function_category()
                elif select_loss_profit < 0:
                    print("你這個奧客")
                    self.close_stock_system()
            function_category()

        # 我的通知
        def my_information():
            account_table.find_element_by_css_selector(
                "a[title='我的通知']").click()

        # 績效對決
        def pk_():
            account_table.find_element_by_css_selector(
                "a[title='績效對決']").click()
            # percentile_rank = self.driver.find_element_by_xpath('//*[@id="TabContent"]/div[2]/div[1]/ul[1]/li[10]').find_element_by_tag_name('b')
            # print(percentile_rank.text)

        # 熱門討論
        def top_discussion():
            account_table.find_element_by_css_selector(
                "a[title='熱門討論']").click()

        # 下單功能
        def trade_stock():
            buy_sell_stock = 0
            stock_code = 0
            stock_quantity = 0
            stock_price = 0
            buy_sell_stock = input("買賣股票\n買請按b\n賣請按s:")
            if buy_sell_stock == "b":
                self.driver.find_element_by_id("Bs_B").click()
            elif buy_sell_stock == "s":
                self.driver.find_element_by_id("Bs_S").click()
            else:
                self.driver.close()

            stock_code = input("輸入股票號碼:")
            self.driver.find_element_by_id("textBoxCommkey").clear()
            self.driver.find_element_by_id("textBoxCommkey").send_keys(
                str(stock_code))
            self.driver.find_element_by_id("textBoxCommkey").click()
            self.driver.find_element_by_id("TextBoxQty").send_keys("輸入張數")
            # self.driver.find_element_by_id("linktofollQtow").click()
            time.sleep(1)

            # 印出五買五賣
            url = "https://www.cmoney.tw/vt/ashx/HandlerGetStockPrice.ashx?q=" + stock_code + "&accountType=7&isSDAndSell=false"
            html = requests.get(url).json()
            best5 = html['StockInfo']

            for i in range(1, 6):
                best5_buy = best5['BuyPr' + str(i)]
                best5_sell = best5['SellPr' + str(i)]
                print('買', i, best5_buy, end=' ')
                print('賣', i, best5_sell, end=' ')
                print('')
            while True:

                while True:
                    try:
                        stock_quantity = int(input("輸入股票張數:"))
                        break
                    except:
                        print("請輸入數字")

                self.driver.find_element_by_id("TextBoxQty").clear()
                self.driver.find_element_by_id("TextBoxQty").send_keys(
                    str(stock_quantity))
                while True:
                    try:
                        stock_price = int(input("輸入買賣之股價:"))
                        break
                    except:
                        print("請輸入數字")
                if stock_quantity <= 0 or stock_price <= 0 or stock_quantity > 499:
                    print("輸入數值有誤")
                else:
                    break

            self.driver.find_element_by_id("TextBoxPrice").clear()
            self.driver.find_element_by_id("TextBoxPrice").send_keys(
                str(stock_price))

            # 下單前確認一下
            run_mother_fucker = str.lower(input("確定要下單嗎?\n如果是請按y\n否請按n:"))
            if run_mother_fucker == "y":
                self.driver.find_element_by_id("Orderbtn").click()

            elif run_mother_fucker == "n":
                continue_or_exit = str.lower(input("要離開程式嗎?\n如果是請按y\n否請按n:"))

                if continue_or_exit == "y":
                    self.close_stock_system()

                elif continue_or_exit == "n":
                    function_category()
            order_search()

            # 下單完看看要不要離開或繼續玩
            continue_or_exit = str.lower(input("要離開程式嗎?\n如果是請按y\n否請按n:"))
            if continue_or_exit == "y":
                self.close_stock_system()
            elif continue_or_exit == "n":
                function_category()

        # 功能選單

        def function_category():
            function_category_ = ""
            while True:
                try:
                    function_category_ = int(
                        input("選擇功能:\n"
                              "1帳戶概覽\n"
                              "2委託查詢\n"
                              "3交易紀錄\n"
                              "4庫存明細\n"
                              "5損益試算\n"
                              "6重新選擇帳號\n"
                              "88離開程式:"))
                    function_category_dict = {
                        "1": view_account,
                        "2": order_search,
                        "3": trade_record,
                        "4": stock_detail,
                        "5": loss_profit,
                        '6': my_information,
                        '7': pk_,
                        '8': top_discussion,
                        '9': trade_stock,
                        '10': self.select_account,
                        '88': self.close_stock_system
                    }
                    function_category_dict[str(function_category_)]()
                    break

                except:
                    if function_category_ > 10 and function_category_ != 88:
                        print("沒有那麼多功能")
                        function_category()
                    elif function_category_ < 1:
                        print("你這個奧客")
                        self.close_stock_system()
                    else:
                        print("請輸入數字")

        function_category()
        self.trade_stock_system()

    def close_stock_system(self):
        self.driver.quit()
        sys.exit(0)
def iex_Gtam_import():
    # set download directory path
    p = {
        'download.default_directory':
        r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\iexGtamFile'
    }
    #add options to browser
    opts.add_experimental_option('prefs', p)

    browser = Chrome(options=opts)
    #maximize browser
    browser.maximize_window()
    browser.get('https://www.iexindia.com/marketdata/G-TAM_Details.aspx')
    # TEST WAIT UNTIL IN SELENIUM
    delay = 60  # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located(
                (By.ID, 'ctl00_InnerContent_ddlPeriod')))
    except TimeoutException:
        print("Loading took too much time!")
    # click on the dropdown button ctl00_InnerContent_ddlPeriod
    iexDrpdwnDam = Select(
        browser.find_elements_by_id("ctl00_InnerContent_ddlPeriod")[0])

    # select element of dropdown by visible text
    iexDrpdwnDam.select_by_visible_text('Yesterday')

    # click on update report ctl00_InnerContent_btnUpdateReport
    browser.find_elements_by_id(
        "ctl00_InnerContent_btnUpdateReport")[0].click()
    # wait until dropdown is opened
    dropdownLinks = []
    delay = 60  # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located(
                (By.ID,
                 'ctl00_InnerContent_reportViewer_ctl05_ctl04_ctl00_ButtonLink'
                 )))
    except TimeoutException:
        print("Loading took too much time!")
    # click on different download option ctl00_InnerContent_reportViewer_ctl05_ctl04_ctl00_ButtonLink
    browser.find_elements_by_id(
        "ctl00_InnerContent_reportViewer_ctl05_ctl04_ctl00_ButtonLink"
    )[0].click()

    srcFileLocation = r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\iexGtamFile'
    destFileLocation = r'C:\Users\dheer\Desktop\wrldc\RTM_BROWSER_AUTOMATION\Dumps\iexGtamFile\Archives'
    destFileName = "DateWiseTrade_"
    moveFilesToArchive(srcFileLocation, destFileLocation, destFileName)
    delay = 60  # seconds
    try:
        myElem = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located((By.LINK_TEXT, 'Excel')))
    except TimeoutException:
        print("Loading took too much time!")
    browser.find_elements(By.LINK_TEXT, "Excel")[0].click()
    print("iex GTAM fetch succesful")

    time.sleep(15)
    browser.close()
wait_time = 5
#first run flag
flag = 0

driver = Chrome('.\\chromedriver.exe')

init_url = "https://app.plus500.com/trade/apple"

wait = WebDriverWait(driver, 10)

url = init_url

driver.get(url)

#press demo
demo_button = driver.find_elements_by_id('demoMode')[0]
demo_button.click()

time.sleep(wait_time)

#user
account = driver.find_elements_by_id('newUserCancel')[0]
account.click()

time.sleep(wait_time)
#log in
#user email
u = driver.find_elements_by_id('email')[0]
u.send_keys('*****@*****.**')

#user pass
Beispiel #18
0
import traceback
from selenium.webdriver import Chrome
import pandas as pd
driver = Chrome('./chromedriver')
# driver = Firefox(executable_path = './geckodriver')
# 打開網址
# url = 'https://www.google.com.tw/maps/search/%E5%93%A5%E5%A4%A7%E8%85%B8%E9%BA%B5%E7%B7%9A/@24.9891249,121.491407,14z/data=!3m1!4b1'
url = 'https://www.google.com.tw/maps'
driver.get(url)
# find -> find_element
# find_all -> find_elements
time.sleep(5)  # 等待1秒
keyword = '大腸麵線'

print('輸入框輸入欲搜尋關鍵字')
mylocation = driver.find_elements_by_id('widget-mylocation')
print(len(mylocation))
while len(mylocation) == 0:
    print('mylocation')
    mylocation = driver.find_elements_by_id('widget-mylocation')
mylocation[0].click()
time.sleep(4)
input = driver.find_element_by_class_name('tactile-searchbox-input')
input.send_keys(keyword)

print('點擊搜尋')
driver.find_element_by_id('searchbox-searchbutton').click()
time.sleep(7)
bfinal = False
page = 1
list_dem = []
class ScrapyOne(object):
    def __init__(self, url):
        super(ScrapyOne, self).__init__()
        # 站点url
        self.url = url
        # 当前爬取的页
        self.page = 1
        # 评价总页数
        self.allPage = 1
        # 使用Chrome浏览器
        self.driver = Chrome(options=option)
        # 重新点击次数
        self.retryNum = 5

    # 翻页
    def nextPage(self):
        try:
            self.page += 1
            # 一下的代码可能由于页面的刷新报错,因此这里使用了try-catch
            # 清空页码输入框
            self.driver.find_elements_by_id("cPageNum")[0].clear()
            # 输入下一页的页码
            self.driver.find_elements_by_id("cPageNum")[0].send_keys(self.page)
            # 点击翻页按钮
            self.driver.find_elements_by_id('cPageBtn')[0].click()
            # 获取当前的页码
            currentPage = int(
                self.driver.find_elements_by_xpath(
                    "//*/a[@class='current']/span")[0].text)
            while currentPage != self.page:
                # 这里的休眠十分重要,最好设置大一点,不然可能什么也爬不到
                time.sleep(3)
                self.driver.find_elements_by_id("cPageNum")[0].clear()
                self.driver.find_elements_by_id("cPageNum")[0].send_keys(
                    self.page)
                self.driver.find_elements_by_id('cPageBtn')[0].click()
                currentPage = int(
                    self.driver.find_elements_by_xpath(
                        "//*/a[@class='current']/span")[0].text)
            self.retryNum = 5  #不知道啥意思
        except Exception as e:
            # 重试次数超过5次,结束翻页
            if self.retryNum == 0:
                return False
            # 如果不是爬到最后一页
            if self.page <= self.allPage:
                # 页码回滚
                self.page -= 1
                # 刷新当前页面
                self.driver.refresh()
                time.sleep(3)
                self.retryNum -= 1
                # 继续翻页
                self.nextPage()

    # 爬取评论信息
    def scrapyComment(self):
        data = etree.HTML(self.driver.page_source)
        path = "//div[@class='comment_block J_asyncCmt']"
        path1 = "//span[@class='small_c']/@data-value"
        path2 = "//span[@class='score']/span/text()"
        path3 = "//span[@class='type']/text()"
        path4 = "//span[@class='date']/text()"
        path5 = "//a[@class='room J_baseroom_link room_link']/text()"
        path6 = "//a[@class='useful']/@data-voted"
        path7 = "//div[@class='J_commentDetail']/text()"
        # 使用Xpath获取评价
        for i in range(1, 16):
            if data.xpath(path + str([i])):
                path1_ = path + str([i]) + path1
                path2_ = path + str([i]) + path2
                path3_ = path + str([i]) + path3
                path4_ = path + str([i]) + path4
                path5_ = path + str([i]) + path5
                path6_ = path + str([i]) + path6
                path7_ = path + str([i]) + path7
                scores = str(data.xpath(path1_)).strip("'[]'")
                sum_score = str(data.xpath(path2_)).strip("''[]")
                types = str(data.xpath(path3_)).strip("''[]")
                date = str(data.xpath(path4_)).strip("'[]'")
                room = str(data.xpath(path5_)).strip("''[]")
                useful = str(data.xpath(path6_)).strip("''[]")
                text = str(data.xpath(path7_)).strip("''[]")

                res = []
                res.extend(
                    [scores, sum_score, types, date, room, useful, text])
                self.save(res)

        print('[%s]\t%s\t第%s页爬取完成' %
              (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                  time.time())), self.url, self.page))

    # 获取当前站点的评价信息总共有多少页
    def getPage(self):
        return int(
            self.driver.find_element_by_xpath(
                "//*[@id='divCtripComment']/div[4]/div/div[1]/a[7]/span").text)

    # 保存评价信息
    def save(self, item):

        filename = str(self.url).split('.')[2].split('/')[2]
        filepath = "D:\PythonCodes\Python_Course_Project/" + filename + '.csv'
        with open(filepath, 'a', newline='', encoding='utf_8_sig') as f:
            writer = csv.writer(f)
            writer.writerow(item)
            f.close()

    # def save(self, line):    #
    #     with open(file, 'a', encoding='UTF-8-sig') as f:
    #         f.write(line + '\n')

    def main(self):
        # 这里会打开Chrome浏览器
        self.driver.get(self.url)
        self.allPage = self.getPage()
        # 点击查看评价的按钮
        # self.driver.find_element_by_id('commentTab').click()
        while self.page <= self.allPage:
            # 爬取评价
            self.scrapyComment()
            # 翻页
            self.nextPage()
        print('爬取完成', self.url)
        # 关闭当前浏览器窗口
        self.driver.close()
# find find_element 找一個
# find_all find_elements 找多個

# 登入google
# driver.find_element_by_id("identifierId").send_keys("")
# driver.find_element_by_id("identifierNext").click()
# time.sleep(1)
# driver.find_element_by_name("password").send_keys("")
# driver.find_element_by_id("passwordNext").click()
# time.sleep(10)

# 拿紙條  bs4:.text ; selenium: .text 
# 拿標籤  bs4:["href"] ; selenium: get_attribute("href")

na = driver.find_elements_by_id("video-title")

# for i in na: 先做一個測試
title = na[0].text
url = na[0].get_attribute("href")
print(title, url)

pl = Playlist(url,suppress_exception=True)  # suppress_exception表示如果下載檔案中有發生檔案遺失(比較久的檔案),請電腦忽視此問題繼續下載剩下的影像。
    
dirname = "youtubelist/"
if not os.path.exists(dirname):
    os.mkdir(dirname)
pl.download_all(dirname)    #依然卡到keyerror: "title"???

time.sleep(60)
driver.close()
Beispiel #21
0
     continue
 table_page = table_page[0]
 ads = table_page.find_elements_by_class_name('bull-item-content')  # Все объявления на страницы(50)
 for elem in ads:  # Перебераем по одному объявлению
     url_currentAds = elem.find_elements_by_class_name('bull-item__self-link')
     if len(url_currentAds) == 0:
         print(404)
         continue
     current_url = url_currentAds[0].get_attribute('href')
     price = elem.find_elements_by_class_name('price-block__price')
     if len(price) == 0:
         driver.execute_script('window.open()')
         driver.switch_to.window(driver.window_handles[1])
         driver.get(current_url)
         delay(10)
         view = driver.find_elements_by_id('fieldsetView')
         view = view[0]
         fields = view.find_elements_by_class_name('value')
         load_data(house_coll, cd_home(current_url, fields[0].text, fields[1].text, fields[4].text), current_url)
     else:
         cost = price[0].text
         driver.execute_script('window.open()')
         driver.switch_to.window(driver.window_handles[1])
         driver.get(current_url)
         delay(10)
         view = driver.find_elements_by_id('fieldsetView')
         view = view[0]
         fields = view.find_elements_by_class_name('value')
         load_data(flat_coll,
                   cd_flat(current_url, cost, fields[1].text, fields[2].text, fields[3].text, fields[5].text),
                   current_url)