python 抄巫妖的招式

hmae6n7t  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(78)

我想创建一个关于国际象棋最佳走法的人工智能建议。我在bs4中没有得到lichess的HTML标签。
在Chrome中我可以看到bs4中的标签,这是一个基于脚本的命令。为了更好地理解,我将展示一些代码。
这是我搜集数据的脚本。

from bs4 import BeautifulSoup
from requests import get
from time import time

def scrape_data_from_lichess(url):
    response = get(url)
    html_soup = BeautifulSoup(response.text, 'html.parser')
    move_played = html_soup.find_all('move')
    print(move_played)

while not scrapeData:
    scrape_data_from_lichess(webpage + url)
    time.sleep(10)

这是我的谷歌开发工具对标签位置的回应。这真的很奇怪,因为Chrome扩展“网页搜罗器”实际上可以检测到这些标签。

<div class="moves">
  <index class="">1</index>
  <move class="">d4</move>
  <move class="">d6</move>
  <index class="">2</index>
  <move class="active">Nf3</move>
  <move class="">Nf6</move>
</div>

最后一个是我的html文本的bs4结果。这是一个简短的版本。我修改了它。

LichessRound.boot({
    data: {"game":{"player":"white","opponent":{"color":"black","name":null,"ai":1},"san":"d4","san":"d6","san":"Nf3",{"san":"Nf6"}]},

以下是详细版本。

LichessRound.boot({
data: {"game":{"id":"4PAVr8QI","variant":{"key":"standard","name":"Standard","short":"Std"},"speed":"correspondence","perf":"correspondence","rated":false,"initialFen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1","fen":"rnbqkb1r/ppp1pppp/3p1n2/8/3P4/5N2/PPP1PPPP/RNBQKB1R w KQkq - 2 3","player":"white","turns":4,"startedAtTurn":0,"source":"ai","status":{"id":20,"name":"started"},"createdAt":1536082009777,"lastMove":"g8f6","opening":{"eco":"A46","name":"Indian Game: Wade-Tartakower Defense","ply":4}},"clock":null,"correspondence":null,"player":{"color":"white","name":null,"user":{"id":"gamelinetv","username":"Gamelinetv","online":true,"perfs":{"correspondence":{"games":0,"rating":1500,"rd":350,"prog":0,"prov":true}}},"rating":1500,"provisional":true,"version":4,"spectator":true},"opponent":{"color":"black","name":null,"ai":1},"orientation":"white","url":{"socket":"/4PAVr8QI/white/socket","round":"/4PAVr8QI/white"},"pref":{"animationDuration":300,"coords":2,"replay":2,"clockTenths":1,"clockBar":true,"highlight":true,"destination":true,"rookCastle":true,"showCaptured":true},"evalPut":false,"steps":[{"ply":0,"uci":null,"san":null,"fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"},{"ply":1,"uci":"d2d4","san":"d4","fen":"rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1"},{"ply":2,"uci":"d7d6","san":"d6","fen":"rnbqkbnr/ppp1pppp/3p4/8/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2"},{"ply":3,"uci":"g1f3","san":"Nf3","fen":"rnbqkbnr/ppp1pppp/3p4/8/3P4/5N2/PPP1PPPP/RNBQKB1R b KQkq - 1 2"},{"ply":4,"uci":"g8f6","san":"Nf6","fen":"rnbqkb1r/ppp1pppp/3p1n2/8/3P4/5N2/PPP1PPPP/RNBQKB1R w KQkq - 2 3"}]},

我知道Selenium可以解决这个问题。但是它启动了一个新的浏览器,我只想粘贴脚本并获得移动的数据。所以我决定使用BS4,但是我对新的想法持开放态度。

v6ylcynt

v6ylcynt1#

详细介绍Wolfgang Fahl关于使用Lichess API的建议。要么使用其中推荐的Python API之一,要么...

使用python-lichess

使用pip install python-lichess进行安装,然后获得给定game的移动:

import lichess.api

game = lichess.api.game('4PAVr8QI')
print(game['moves'])

图纸:

d4 d6 Nf3 Nf6

相关问题