---
# System prepended metadata

title: 阿瓦隆爬蟲專案(二代目)

---

# 阿瓦隆爬蟲專案(二代目)
## 概述
這篇主要功能為成果展示
下面的程式已經跟一代比整理得較乾淨
目前是沒有其他可以再改進的地方(應該啦)
聽說之後這個網頁會出2.0
可能之後出的時候會再寫一個專案
by 作者:布叮咚瓜茶(高鼎鈞)
## 輸出簡述
下面是擷取的網站畫面
https://avalon.signage-cloud.org/
下面的順序是: 派票+結果 ==> 身分 ==> 玩家
派票的紀錄方式為: 幾號位 ==> 人選 ==> 誰場內黑 ==>誰場外白
如果該結果成功執行,則會顯示結果,OX為成功與失敗的張數
最後顯示身分為:刺客 莫甘娜 默德雷德 奧伯倫/梅林 派西維爾
最後顯示1家~10家的ID
## 範例
### 主介面
![身分](https://hackmd.io/_uploads/HJyfPz7Ua.jpg)
### 派票
#### 第一輪
![1r](https://hackmd.io/_uploads/SJE1_fQIa.jpg)
#### 第二輪
![2r](https://hackmd.io/_uploads/BJYWuz7UT.jpg)
#### 第三輪
![3r](https://hackmd.io/_uploads/HJMzuz7L6.jpg)
#### 第四輪
![4r](https://hackmd.io/_uploads/H1SQdf7Ia.jpg)
## 程式結果
![end new](https://hackmd.io/_uploads/HyB82MQLT.jpg)
# 程式碼
```python=
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import re

def everyonedo(allwhodo):
    anw=[] #出去人選
    aib=[] #內黑
    aow=[] #外白
    whodo = [whodo.text for whodo in allwhodo if whodo.text != ""]
    for e in range(len(whodo)//10):
        nw = 0
        ib = 0
        ow = 0
        u10 = 0
        for r in range(10):
            
            if whodo[10*e+r] == "正常白":
                if r==9:
                    r=-1
                nw = nw*10 + r+1
            elif whodo[10*e+r] == "場內黑":
                if r==9 and ib!=0:
                    r=-1
                    u10 = 1
                ib = ib*10 + r+1
                nw = nw*10 + r+1
            elif whodo[10*e+r] == "場外白":
                if r==9 and ow!=0:
                    u10 = 1
                    r=-1
                ow =  ow*10 + r+1
        if e == 4:
            nw = -2
        if ib == 0 and u10==0:
            ib=-1
        if ow == 0 and u10==0:
            ow=-1
        anw.append(nw)
        aib.append(ib)
        aow.append(ow)
    anw.append(-3) #檢測換下一輪
    return anw,aib,aow

def extract_numbers(input_string):
    pattern = r'\d+'
    numbers = re.findall(pattern, input_string)
    try:
        number = int(numbers[0])
    except:
        number=""
    return number

def main():
    #開啟對局
    browser = webdriver.Chrome()
    browser.get("https://avalon.signage-cloud.org/")
    time.sleep(1)
    name = browser.find_element(By.CLASS_NAME,'form-control')
    name.send_keys("牌譜機器人")
    time.sleep(1)
    login_button = browser.find_element(By.CLASS_NAME, "btn.btn-lg.btn-primary.btn-block.btn-signin")
    login_button.click()
    time.sleep(1)
    room_button = browser.find_element(By.CSS_SELECTOR, "body > div > div > div:nth-child(1) > div.col-md-9 > ul > li > div > div:nth-child(3) > button")
    room_button.click()
    
    #抓取使用者
    time.sleep(1)
    goti = [goti.text for goti in browser.find_elements(By.XPATH, '//p[@align="center"]') if goti.text != ""]
    goti[6], goti[8] = goti[8], goti[6]
    goti[7], goti[9] = goti[9], goti[7]

    #預先抓成功與不成功(順邊抓玩家身分)
    jpgnum=browser.find_elements(By.CLASS_NAME,"img-rounded")
    tf = []
    inplay=[]
    for i in range(len(jpgnum)):
        if jpgnum[i].get_attribute('src') =="https://avalon.signage-cloud.org/image/good_cup.jpg":
            tf.append("O")
        elif jpgnum[i].get_attribute('src') =="https://avalon.signage-cloud.org/image/bad_cup.jpg":
            tf.append("X")
        else:
            inplay.append(jpgnum[i].get_attribute('src'))
    
    #抓尾派內容
    fifgo=[fifgo.text for fifgo in browser.find_elements(By.CLASS_NAME,"btn.btn-primary") if fifgo.text != ""]
    fifuse = []
    del fifgo[0]
    for i in range(len(fifgo)):
        fifgo[i]=extract_numbers(fifgo[i])
        if fifgo[i] == 10:
            fifgo[i] = 0
    try:
        fifuse.append(fifgo[0]*100+fifgo[1]*fifgo[2])
        fifuse.append(fifgo[3]*1000+fifgo[4]*100+fifgo[5]*fifgo[6])
        fifuse.append(fifgo[7]*1000+fifgo[8]*100+fifgo[9]*fifgo[10])
        fifuse.append(fifgo[11]*10000+fifgo[12]*1000+fifgo[13]*100+fifgo[14]*fifgo[15])
        fifuse.append(fifgo[16]*10000+fifgo[17]*1000+fifgo[18]*100+fifgo[19]*fifgo[20])
    except:
        pass
    
    #按按鈕(派票紀錄)
    whogobotton=browser.find_element(By.CSS_SELECTOR,"body > div.container-fluid > div > div.col-sm-7 > div:nth-child(2) > div:nth-child(1) > button")
    whogobotton.click()
    
    #抓取派票
    pnw=[]
    pib=[]
    pow=[]
    for i in range(1,6):

        try:
            time.sleep(1)
            gsb = browser.find_element(By.CSS_SELECTOR, f"#game_statistics_modal > div > div > div.modal-body > div > ul > li:nth-child({i}) > a") #偵測局數按鈕
            gsb.click()
            allodo = browser.find_elements(By.CSS_SELECTOR, "span[style='color: green;'], span[style='color: red;']")
            gnw,gib,gow=everyonedo(allodo)
            pnw=pnw+gnw
            pib=pib+gib
            pow=pow+gow
        except:
            pass

    #扣掉多於黑白球
    if len(tf)==26:
        tf = tf[5:]
        gm=5
    elif len(tf)==21:
        tf = tf[5:]
        gm=4
    else:
        tf=tf[4:]
        gm=3
    #輸出
        
    #輸出派票
    whodothis=0
    for i in range(gm):
        for j in range(6):
            if pnw[0] == -3:
                del pnw[0]
                break
            whodothis = whodothis +1
            if pnw[0] == -2:
                pnw[0] = fifuse[i]
            if pow[0] == -1 and pib[0]==-1:
                print(whodothis%10,".",pnw[0], sep='')
            elif pow[0] == -1 :
                print(whodothis%10,".",pnw[0]," ",pib[0],"-", sep='')
            elif pib[0] == -1:
                print(whodothis%10,".",pnw[0]," ",pow[0],"+", sep='')
            else:
                print(whodothis%10,".",pnw[0]," ",pib[0],"- ",pow[0],"+", sep='')
            del pow[0]
            del pib[0]
            del pnw[0]
        if i == 0 :
            print(tf[0],tf[1],tf[2], sep='')
        if i == 1 :
            print(tf[3],tf[4],tf[5],tf[6], sep='')
            print("")
        if i == 2 :
            print(tf[7],tf[8],tf[9],tf[10], sep='')
            print("")
        if i == 3 :
            print(tf[11],tf[12],tf[13],tf[14],tf[15], sep='')
            print("")
        if i == 4 :
            print(tf[16],tf[17],tf[18],tf[19],tf[20], sep='')
            print("")

    #輸出身分
    goodp=0
    badp=0
    inplay[7], inplay[9] = inplay[9], inplay[7]
    inplay[6], inplay[8] = inplay[8], inplay[6]
    inplay[8], inplay[9] = inplay[9], inplay[8]
    for k in range(9):
        if inplay[k]=="https://avalon.signage-cloud.org/image/Q_%E6%A2%85%E6%9E%97.jpg":
            goodp = goodp+(k+1)%10*10
        elif inplay[k]=="https://avalon.signage-cloud.org/image/Q_%E6%B4%BE%E8%A5%BF%E7%B6%AD%E7%88%BE.jpg":
            goodp = goodp+(k+1)%10
        elif inplay[k]=="https://avalon.signage-cloud.org/image/Q_%E5%88%BA%E5%AE%A2.jpg":
            badp = badp+(k+1)%10*1000
        elif inplay[k]=="https://avalon.signage-cloud.org/image/Q_%E8%8E%AB%E7%94%98%E5%A8%9C.jpg":
            badp = badp+(k+1)%10*100
        elif inplay[k]=="https://avalon.signage-cloud.org/image/Q_%E8%8E%AB%E5%BE%B7%E9%9B%B7%E5%BE%B7.jpg":
            badp = badp+(k+1)%10*10
        elif inplay[k]=="https://avalon.signage-cloud.org/image/Q_%E5%A5%A7%E4%BC%AF%E5%80%AB.jpg":
            badp = badp+(k+1)%10
    if goodp//10 == 0:
        print(badp,"/","0",goodp,sep="")
    elif badp//1000 == 0:
        print("0",badp,"/",goodp,sep="")
    else:
        print(badp,"/",goodp,sep="")
    
    #輸出玩家
    for i, player in enumerate(goti):
        print(i + 1, ".", player,sep="")

    #關閉網頁
    browser.quit()

    
if __name__ == "__main__":
    main()

```