Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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 使用BeautifulSoup删除播放器数据_Python_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup删除播放器数据

Python 使用BeautifulSoup删除播放器数据,python,beautifulsoup,Python,Beautifulsoup,我正试图通过BeautifulSoup获得足球数据,我正试图从“a”标签上获得球员的名字,但却没有运气 这是我目前的代码: from bs4 import BeautifulSoup import requests import numpy as np import pandas as pd url = 'https://www.pro-football-reference.com/years/2020/rushing.htm#rushing_and_receiving::rush_yds'

我正试图通过BeautifulSoup获得足球数据,我正试图从“a”标签上获得球员的名字,但却没有运气

这是我目前的代码:

from bs4 import BeautifulSoup
import requests
import numpy as np
import pandas as pd

url = 'https://www.pro-football-reference.com/years/2020/rushing.htm#rushing_and_receiving::rush_yds'

req = requests.get(url).text
soup = BeautifulSoup(req, 'lxml')
players = []
table = soup.find('table')
rows = table.find_all('tr')
for row in rows:
    player = row.find_all('td', {'data-stat':'player'})
    print(player)
以下是返回内容的前几个示例:

[<td class="left" csk="Henry,Derrick" data-append-csv="HenrDe00" data-stat="player"><a href="/players/H/HenrDe00.htm">Derrick Henry </a>*</td>]
[<td class="left" csk="Cook,Dalvin" data-append-csv="CookDa01" data-stat="player"><a href="/players/C/CookDa01.htm">Dalvin Cook</a>*</td>]
[<td class="left" csk="Jacobs,Josh" data-append-csv="JacoJo01" data-stat="player"><a href="/players/J/JacoJo01.htm">Josh Jacobs</a>*</td>]
我尝试了以下循环,但出现错误:

for row in rows:
        player = row.find_all('td', {'data-stat':'player'}).text
        print(player)

谢谢

在获取名称的循环中,
find_all
返回一个
ResultSet
(这是使用选择器找到的元素的
列表)。您想改用
find

for row in rows:
    player = row.find('td', {'data-stat':'player'})
    if player:
        player = player.text
        print(player)

在获取名称的循环中,
find_all
返回一个
ResultSet
(这是使用选择器找到的元素的
列表)。您想改用
find

for row in rows:
    player = row.find('td', {'data-stat':'player'})
    if player:
        player = player.text
        print(player)

谢谢你的帮助!谢谢你的帮助!