写一下爬取虎扑NBA球员信息的爬虫代码
时间: 2024-01-21 09:03:19 浏览: 174
好的,以下是一个简单的爬取虎扑NBA球员信息的Python爬虫代码:
```python
import requests
from bs4 import BeautifulSoup
# 定义爬虫函数
def spider(url):
# 发送请求
response = requests.get(url)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 获取球员信息
players = soup.select('.players-list .player-card')
for player in players:
# 获取球员姓名
name = player.select('.player-card-name')[0].get_text()
# 获取球员头像链接
avatar = player.select('.player-card-image img')[0]['src']
# 获取球员所属球队
team = player.select('.player-card-team')[0].get_text()
# 获取球员位置
position = player.select('.player-card-position')[0].get_text()
# 获取球员个人主页链接
profile_url = player.select('.player-card-name a')[0]['href']
# 输出球员信息
print('姓名:', name)
print('头像链接:', avatar)
print('所属球队:', team)
print('位置:', position)
print('个人主页链接:', profile_url)
print('---------------------')
# 主函数
if __name__ == '__main__':
# 爬取前10页的球员信息
for i in range(1, 11):
url = f'https://nba.hupu.com/players?page={i}'
spider(url)
```
通过以上代码,我们可以通过爬虫程序爬取虎扑NBA球员信息并打印输出。
阅读全文