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.

66 lines
2.7 KiB

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