我只想返回这3名球员的名字(在网址中)。。当前代码返回他们的姓名、球队和篮球协会。我是否可以在代码中指定只返回名称?你知道吗
从here刮取数据:
import requests
from bs4 import BeautifulSoup
def bball_spider(str):
source_code = requests.get(str)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
# Players
for elements in soup.find('table' , {'id' : 'stats'}).findAll('a'):
names = elements.string
print(names)
str = input("Enter the Query Result URL ")
bball_spider(str)
您就快到了,但首先让我提一下这一点,因为您似乎是Python的新手:您不应该命名变量
str
,因为它隐藏了内置的str类,所以我在下面的代码中修改了这一点。重要的修改是我将您的.findAll('a')
更改为.findAll('td',{'class':'left active'})
,检查元素我们可以看到所有玩家的名字都在一个带有类left active
的<td>
标记中。我还将迭代var改为element
,而不是复数形式,因此从语义上讲它更有意义。还要注意的是,你张贴的代码没有正确识别,但我认为这只是一个格式问题,当你粘贴在这里。你知道吗这将打印:
相关问题 更多 >
编程相关推荐