Pulls repairability scores for various devices from iFixit, which are then displayed on a website in a few categories.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.0 KiB

5 years ago
  1. import time
  2. from bs4 import BeautifulSoup
  3. import requests
  4. import re
  5. def get_device_manufacturers(device_names):
  6. list_of_manufacturers_without_tags = []
  7. for i in device_names:
  8. i = str(i)
  9. i = i.replace('<div class="cell device-name">\n','')
  10. i = i.lstrip()
  11. i = i.split('<')[0]
  12. i = i.rstrip()
  13. list_of_manufacturers_without_tags.append(i)
  14. return list_of_manufacturers_without_tags
  15. def get_device_models(device_names, soup):
  16. list_of_models_without_tags = []
  17. list_of_models = soup.findAll("span", {"class": "selected"})
  18. for i in list_of_models:
  19. i = str(i)
  20. i = i.replace('<span class="selected">','').replace('</span>','')
  21. list_of_models_without_tags.append(i)
  22. return list_of_models_without_tags
  23. def format_device_scores(device_scores, soup):
  24. list_of_scores_without_tag = []
  25. list_of_scores = soup.findAll("h3")
  26. for i in list_of_scores:
  27. i = str(i)
  28. i = i.replace('<h3>','').replace('</h3>','')
  29. list_of_scores_without_tag.append(i)
  30. return list_of_scores_without_tag
  31. def data_from_each(list_of_manufacturers_without_tags, list_of_models_without_tags, list_of_scores_without_tag):
  32. j = 0
  33. for i in list_of_manufacturers_without_tags:
  34. print(list_of_manufacturers_without_tags[j], list_of_models_without_tags[j], list_of_scores_without_tag[j])
  35. j = j + 1
  36. def main():
  37. link = "https://www.ifixit.com/smartphone-repairability"
  38. page = requests.get(link)
  39. soup = BeautifulSoup(page.content, 'html.parser')
  40. device_names = soup.findAll("div", {"class": "cell device-name"})
  41. device_scores = soup.findAll("div", {"class": "cell device-score"})
  42. list_of_manufacturers_without_tags = get_device_manufacturers(device_names)
  43. list_of_models_without_tags = get_device_models(device_names, soup)
  44. list_of_scores_without_tag = format_device_scores(device_scores, soup)
  45. data_from_each(list_of_manufacturers_without_tags, list_of_models_without_tags, list_of_scores_without_tag)
  46. if __name__ == '__main__':
  47. main()