Python简易爬虫小实例:爬取NBA球队赛季对阵数据 !

之前浏览《Python数据挖掘入门与实践》这本书的时候发现了非常有意思的内容——用决策树预测NBA获胜球队,但是书中获得原始数据的方式已经行不通了,所以一直没有能够重复这一章的内容。恰巧最近发现了一个利用Python BeautifulSoup模块抓取NBA选秀数据的教程 Learning Python: Part 1:Scraping and Cleaning the NBA draft。突然意识到是否可以利用这份教程来抓取NBA球队的对阵数据,从而重复利用决策树越策NBA获胜球队的内容。

 

第一部分

这部分内容来自参考书《Python网络数据采集》第一章的内容 基本流程:通过urlopen()函数获得网页的的全部HTML代码;然后通过BeautifulSoup模块解析HTML代码获得我们想要的内容

 
 
  1. from urllib.request import urlopen

  2. url = "http://pythonscraping.com/pages/page1.html"

  3. html = urlopen(url)

  4. print(html.read())

输出结果

 
 
  1. b'<html>\n<head>\n<title>A Useful Page</title>\n</head>\n<body>\n<h1>An Interesting Title</h1>\n<div>\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n</div>\n</body>\n</html>\n'

简易理解html源代码:尖括号<>内是标签,两个尖括号中间是内容 BeautifulSoup解析

 
 
  1. from bs4 import BeautifulSoup

  2. soup = BeautifulSoup(html)

如果我们想要获得以上html源代码中title中的内容

 
 
  1. soup.title

  2. soup.findAll("title")

  3. soup.title.getText()

 

第二部分

爬取2013-2014赛季NBA球队的对阵数据

 
 
  1. from urllib.request import urlopen

  2. from bs4 import BeautifulSoup

  3. import pandas as pd

  4. import time

  5. start = time.time()

  6. months = ["october","november","december","january","february","march","april","may","june"]

  7. col_header = ["Date","Start(ET)","Visitor/Neutral","PTS","Home/Neutral","PTS","","","Attend","Notes"]

  8. url = "https://www.basketball-reference.com/leagues/NBA_2014_games-"

  9. NBA_1314_Schedule_and_results = []

  10. for month in months:

  11. urls = url + month + ".html"

  12. html = urlopen(urls)

  13. soup = BeautifulSoup(html,"lxml")

  14. start_1 = time.time()

  15. print(month)

  16. for i in range(len(soup.tbody.findAll("tr"))):

  17. Schedule = []

  18. date = soup.tbody.findAll("tr")[i].findAll("th")[0].getText()

  19. Schedule.append(date)

  20. for j in range(len(soup.findAll("tr")[i].findAll("td"))):

  21. data = soup.findAll("tr")[i].findAll("td")[j].getText()

  22. Schedule.append(data)

  23. NBA_1314_Schedule_and_results.append(Schedule)

  24. end_1 = time.time()

  25. print(month,round(end_1 - start_1,2),"s")

  26. df = pd.DataFrame(NBA_1314_Schedule_and_results,columns = col_header)

  27. end = time.time()

  28. print("The total time used:",round(end - start,2),"s")

  29. df.to_csv("NBA_2013_2014_Schedule_and_results.csv")

成功

部分结果

结果中存在的问题

每个月份开始的第一行没有数据,暂时还没有发现是什么原因

猜你喜欢

转载自blog.csdn.net/qq_42156420/article/details/88948744