赞
踩
import requests
from lxml import etree
url="https://nba.hupu.com/stats/players"
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
resp=requests.get(url,headers=headers)
e = etree.HTML(resp.text)
nos=e.xpath('//table[@class="players_table"]//tr//td[1]//text()')
names=e.xpath('//table[@class="players_table"]//tr/td[2]/a/text()')
teams=e.xpath('//table[@class="players_table"]//tr/td[3]/a/text()')
scores=e.xpath('//table[@class="players_table"]//tr/td[4]/text()') #注意这里是没有a标签的
with open("nba.txt",mode="w",encoding="utf-8") as f:
i=0;
for no, name, team, score in zip(nos[1:], names, teams, scores[1:]):
# print(f'名次:{no} 姓名:{name} 球队:{team} 分数:{score}')
f.write(f'名次:{no} 姓名:{name} 球队:{team} 分数:{score}\n')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。