From c3a5b157dc375c27e78eae48b4b25457333517b3 Mon Sep 17 00:00:00 2001 From: pbqs4lvtu <1779073549@qq.com> Date: Sun, 16 Jun 2024 18:25:47 +0800 Subject: [PATCH] Update README.md --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bae815a..0190067 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,23 @@ -# importrequests +import requests +import parsel +import csv +with open("data.csv", mode="a", encoding="utf-8", newline="") as f: + csv_writer = csv.DictWriter(f, fieldnames=["排名", "名字", "球队", "得分", "场次"]) + if f.tell() == 0: + csv_writer.writeheader() + url = "https://nba.hupu.com/stats/players" + response = requests.get(url=url) + html_data = response.text + selector = parsel.Selector(html_data) + trs = selector.xpath('//tbody/tr[position()>1]') + for tr in trs: + ranking = tr.xpath("./td[1]/text()").get() # 获取排名 + name = tr.xpath("./td[2]//a/text()").get() # 获取名字 + team = tr.xpath("./td[3]//a/text()").get() # 获取球队 + score = tr.xpath("./td[4]/text()").get() # 获取得分 + venue = tr.xpath("./td[11]/text()").get() # 获取场次 + print(f"排名: {ranking}, 名字: {name}, 球队: {team}, 得分: {score}, 场次: {venue}") + data_dict = {"排名": ranking,"名字": name,"球队": team,"得分": score,"场次": venue + } + csv_writer.writerow(data_dict) \ No newline at end of file