scrape ICC ranking teams with python


SUBMITTED BY: Charity

DATE: May 11, 2020, 10:19 p.m.

FORMAT: Text only

SIZE: 1.2 kB

HITS: 475

  1. from bs4 import BeautifulSoup
  2. import requests
  3. import pandas as pd
  4. url = "https://www.icc-cricket.com/rankings/mens/team-rankings/test"
  5. # puting headers to make a crawler looks human like + using request lib
  6. headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit 537.36 (KHTML, like Gecko) Chrome",
  7. "Accept": "text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,*/*;q=0.8"}
  8. response = requests.get(url, headers = headers)
  9. ranking = BeautifulSoup(response.text, "html.parser")
  10. rank = ranking.find("div", {"class":"rankings-block__container rankings-table"})
  11. pos = 1
  12. my_list = []
  13. for team in rank.findAll("td", {"class": "table-body__cell rankings-table__team u-text-left"}):
  14. name = team.text.replace(" ", "")
  15. name = team.text.replace("\n", "")
  16. print("%d"%pos,"%s" % name)
  17. my_list.append([pos, name])
  18. pos +=1
  19. # creting a csv file to insert the data
  20. data_frame = pd.DataFrame(my_list, columns=["Ranking", "Team"])
  21. # you can now use data_frame.head(), data_frame.tail()
  22. # inserting data in to a csv file
  23. data_frame.to_csv("Top 10 cricket teams.csv", )
  24. print(data_frame)

comments powered by Disqus