Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得NBA的首发阵容?_Python_Xpath_Web Scraping_Lxml - Fatal编程技术网

Python 如何获得NBA的首发阵容?

Python 如何获得NBA的首发阵容?,python,xpath,web-scraping,lxml,Python,Xpath,Web Scraping,Lxml,我对网页抓取还不熟悉,需要一些帮助。我想使用Xpath来获取NBA的首发阵容、球队和球员的位置。我只是从名字开始,因为我遇到了一个问题 以下是我目前的代码: from urllib.request import urlopen from lxml.html import fromstring url = "https://www.lineups.com/nba/lineups" content = str(urlopen(url).read()) comment = content.re

我对网页抓取还不熟悉,需要一些帮助。我想使用Xpath来获取NBA的首发阵容、球队和球员的位置。我只是从名字开始,因为我遇到了一个问题

以下是我目前的代码:

from urllib.request import urlopen
from lxml.html import fromstring 


url = "https://www.lineups.com/nba/lineups"

content = str(urlopen(url).read())
comment = content.replace("-->","").replace("<!--","")
tree = fromstring(comment)


for nba, bball_row in enumerate(tree.xpath('//tr[contains(@class,"t-content")]')):
    names = bball_row.xpath('.//span[@_ngcontent-c5="long-player-name"]/text()')[0]
    print(names)
从urllib.request导入urlopen
从lxml.html导入fromstring
url=”https://www.lineups.com/nba/lineups"
content=str(urlopen(url).read())

comment=content.replace(“-->”和“”).replace(位于
script
节点内的所需内容

<script nonce="STATE_TRANSFER_TOKEN">window['TRANSFER_STATE'] = {...}</script>
更新

我想我只是把它复杂化了:)

它应该简单如下:

import requests

source = requests.get("https://api.lineups.com/nba/fetch/lineups/gateway").json()
for player in source['data'][0]['away_players']:
        print(player['name'])
更新2

要获得所有团队阵容,请使用以下命令:

import requests

source = requests.get("https://api.lineups.com/nba/fetch/lineups/gateway").json()

for team in source['data']:
    print("\n%s players\n" % team['home_route'].capitalize())
    for player in team['home_players']:
        print(player['name'])
    print("\n%s players\n" % team['away_route'].capitalize())
    for player in team['away_players']:
        print(player['name'])

您要刮取的数据是通过Javascript注入的。您不能按照您正在刮取的方式进行刮取。请尝试查看Selenium。谢谢@aris!我将研究Selenium,如果我发现它,将更新我的代码。做得很好。您是如何发现指向所需结果的链接可能位于任何脚本@sir Anderson内的?有吗使用dev tools有什么诀窍吗?@SIM,首先要做的就是复制部分所需数据->打开页面源代码(右键单击页面->查看页面源代码)->Ctrl+F->Ctrl+V。如果数据不在那里,那么它很可能来自XHR。但在本例中,数据以JSON格式存在于
script
节点中,JSON的第一个键是指向API的链接。没有魔法:)这就像广告中所说的那样完美工作……非常感谢@Andersson的帮助。了不起的工作!=)我刚刚注意到所有团队nd球员没有被打印出来。我最初认为这是因为所有的先发球员都还没有加入,但先发球员现在已经更新。我看到上面的代码打印了7支球队,顶级球队没有头衔。有什么建议吗@Andersson?我非常感谢你的帮助,先生。@AbleArcher,嗯……我有6支球队参加了当前的3场比赛……Can由于我不确定您想要得到什么输出,您是否澄清了输出的具体错误?请确保您使用的是第二个更新块中的代码
import requests

source = requests.get("https://api.lineups.com/nba/fetch/lineups/gateway").json()
for player in source['data'][0]['away_players']:
        print(player['name'])
import requests

source = requests.get("https://api.lineups.com/nba/fetch/lineups/gateway").json()

for team in source['data']:
    print("\n%s players\n" % team['home_route'].capitalize())
    for player in team['home_players']:
        print(player['name'])
    print("\n%s players\n" % team['away_route'].capitalize())
    for player in team['away_players']:
        print(player['name'])