【Selenium_Web_混合定位】(美團)大衆點評霸王餐免費抽取,網址爬蟲+自動報名,自動化測試案例

#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2019-09-10 and 12:32
FileName:
Description:大衆點評自動報名...
'''
from selenium import webdriver
from time import sleep
from pprint import pprint
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome(r"D:\TestFiles\Python3\chromedriver.exe")
driver.implicitly_wait(2)
driver.get(r"http://s.dianping.com/event/chengdu")
more = driver.find_element_by_xpath("//a[@class='load-more show']")
mores = more.get_attribute("class")
addList = ['春熙路']
addStr = "雙人"
successList = []
failList = []

# 主窗口柄和標題
mainWindows = driver.current_window_handle
mainTitle = driver.title
print(f"\n---- 1.Welcome,歡迎進入霸王餐首頁:{mainTitle} ----\n")
sleep(5)

# 加載更多
more = driver.find_element_by_css_selector(".load-more.show")
# print(more)
try:
    while True:
        if more.get_attribute("class") == r"load-more show":
            print(f"-- 點擊加載更多成功,請稍等片刻!--")
            more.click()
            sleep(1)
        elif more.get_attribute("class") == r"load-more":
            print("\n---- 2.Success!霸王餐信息已全部加載完畢!開始循環獲取商鋪信息----\n")
            break
except:
    print("-- 點擊加載更多失敗,請檢查!--")
count = 0
menus = driver.find_elements_by_css_selector(".activity-lists-wraper li")
for i in menus:
    url = i.find_element_by_css_selector("a")
    storeUrl = i.find_element_by_css_selector("a").get_attribute("href")
    storeName = i.find_element_by_css_selector("h4").text
    storeAddr = i.find_element_by_css_selector("div.addi>span.addr").text
    count +=1
    # if storeAddr in addList:
    #     print(f"{count:<3}.附近商家姓名:{storeName} || 地址:{storeAddr} || 鏈接:{storeUrl}")
    if addStr in storeName or r"飲品" in storeName:
        print(f"{count:<3}.商家姓名:{storeName} || 地址:{storeAddr} || 鏈接:{storeUrl}")
        url.click()

        # 跳轉新窗口
        for window in driver.window_handles:
            driver.switch_to.window(window)
            if window != mainWindows:
                break
                #--
        sleep(1.5)
        button = driver.find_element_by_css_selector(".btn-txt.J_self_apply")
        # print(f"新標籤頁面按鈕爲:{button.text}")
        sleep(1.5)
        button.click()
        sleep(5)# 手動登錄時間
        try:
            # abert = driver.find_element_by_css_selector(".pop-main")
            # print(f"新標籤頁面彈框爲:{abert.text}")
            # abert.click()
            driver.find_element_by_css_selector("#J_pop_ok").click()
            sleep(1)
            abert1 = driver.find_element_by_css_selector(".pop-main .col.tit")
            print(f"報名成功頁面彈框爲:{abert1.text}\n")
            sleep(1)
            # 關閉彈窗
            driver.find_element_by_css_selector(".hd a.close").click()
            sleep(1.5)
            successList.append(f"{count}.{storeName}|{storeUrl}")
        except Exception as e:
            print(e)
            print(f"報名異常,請人工檢查連接地址:{count}.{storeName}:{storeUrl}\n")
            failList.append(f"{count}.{storeName}|{storeUrl}")
        finally:
            # 關閉新標籤
            driver.close()


        # 切回主窗口
        driver.switch_to.window(mainWindows)
        # print(f"{count}.{storeName}店鋪報名成功,成功回到主窗口:{driver.title}")
print(f"報名成功店鋪總數:{len(successList)}")
for sucess in  successList:
    print(f"\n---- 3.店鋪詳情:{sucess} ----\n")

print(f"報名成功店鋪總數:{len(failList)}")
for fail in  failList:
    print(f"\n---- 4.店鋪詳情:{fail} ----\n")



----------------------執行結果:由於第一次執行已經報名成功了70店鋪,重新跑一次,不能再次報名,所以提示異常------------------

---- 1.Welcome,歡迎進入霸王餐首頁:成都本地同城活動-成都霸王餐-大衆點評網 ----

-- 點擊加載更多成功,請稍等片刻!--
-- 點擊加載更多成功,請稍等片刻!--
-- 點擊加載更多成功,請稍等片刻!--

---- 2.Success!霸王餐信息已全部加載完畢!開始循環獲取商鋪信息----

1  .商家姓名:【VIP專享】肉桂廚房CINNAMON精緻雙人餐免費搶 || 地址:寬窄巷子 || 鏈接:http://s.dianping.com/event/848277703
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:1.【VIP專享】肉桂廚房CINNAMON精緻雙人餐免費搶:http://s.dianping.com/event/848277703

2  .商家姓名:【VIP專享】李八哥成都小盤菜火鍋超值雙人套餐免費搶 || 地址:高新區 || 鏈接:http://s.dianping.com/event/1714192068
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:2.【VIP專享】李八哥成都小盤菜火鍋超值雙人套餐免費搶:http://s.dianping.com/event/1714192068

3  .商家姓名:丸摩堂夏季福利招牌飲品免費搶 || 地址:多商圈 || 鏈接:http://s.dianping.com/event/320629907
Message: no such element: Unable to locate element: {"method":"css selector","selector":".pop-main .col.tit"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:3.丸摩堂夏季福利招牌飲品免費搶:http://s.dianping.com/event/320629907

11 .商家姓名:書亦燒仙草雙人飲品套餐免費搶 || 地址:多商圈 || 鏈接:http://s.dianping.com/event/319838408
Message: no such element: Unable to locate element: {"method":"css selector","selector":".pop-main .col.tit"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:11.書亦燒仙草雙人飲品套餐免費搶:http://s.dianping.com/event/319838408

15 .商家姓名:【VIP專享】堂前燕價值150元雙人餐免費搶 || 地址:武侯祠 || 鏈接:http://s.dianping.com/event/234374367
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:15.【VIP專享】堂前燕價值150元雙人餐免費搶:http://s.dianping.com/event/234374367

16 .商家姓名:小妖籤串串香超值雙人餐免費搶 || 地址:北門大橋 || 鏈接:http://s.dianping.com/event/618443768
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:16.小妖籤串串香超值雙人餐免費搶:http://s.dianping.com/event/618443768

20 .商家姓名:【吃住玩全包】價值3644元漢唐·馨閣民宿雙人三天兩晚休閒遊 || 地址:西山公園 || 鏈接:http://s.dianping.com/event/884865365
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

報名異常,請人工檢查連接地址:

20.【吃住玩全包】價值3644元漢唐·馨閣民宿雙人三天兩晚休閒遊:http://s.dianping.com/event/884865365


報名異常店鋪總數:7

---- 4.店鋪詳情:1.【VIP專享】肉桂廚房CINNAMON精緻雙人餐免費搶|http://s.dianping.com/event/848277703 ----


---- 4.店鋪詳情:2.【VIP專享】李八哥成都小盤菜火鍋超值雙人套餐免費搶|http://s.dianping.com/event/1714192068 ----


---- 4.店鋪詳情:3.丸摩堂夏季福利招牌飲品免費搶|http://s.dianping.com/event/320629907 ----


---- 4.店鋪詳情:11.書亦燒仙草雙人飲品套餐免費搶|http://s.dianping.com/event/319838408 ----


---- 4.店鋪詳情:15.【VIP專享】堂前燕價值150元雙人餐免費搶|http://s.dianping.com/event/234374367 ----


---- 4.店鋪詳情:16.小妖籤串串香超值雙人餐免費搶|http://s.dianping.com/event/618443768 ----


---- 4.店鋪詳情:20.【吃住玩全包】價值3644元漢唐·馨閣民宿雙人三天兩晚休閒遊|http://s.dianping.com/event/884865365 ----


Process finished with exit code 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章