From 246ad4603375df8e128e57f8281d9537c697bc5a Mon Sep 17 00:00:00 2001 From: rav4s Date: Wed, 20 Jan 2021 13:14:16 -0600 Subject: [PATCH] Added function to write data to csv file --- list.csv | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ scraper.py | 15 ++++++- 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 list.csv diff --git a/list.csv b/list.csv new file mode 100644 index 0000000..39ded49 --- /dev/null +++ b/list.csv @@ -0,0 +1,115 @@ +Manufacturer,Model,Score +iPhone,12 Pro,6 +Microsoft,Surface Duo,2 +Samsung,Galaxy Note 20 Ultra,3 +iPhone,SE 2020,6 +Samsung,Galaxy A51,4 +Samsung,Galaxy S20 Ultra,3 +Samsung,Galaxy Z Flip,2 +Motorola,razr,1 +Google,Pixel 4 XL,4 +iPhone,11,6 +iPhone,11 Pro Max,6 +Fairphone,3,10 +Samsung,Galaxy Note10 Plus 5G,3 +Shift,6m,9 +Huawei,Mate 20 X 5G,4 +Google,Pixel 3a XL,6 +Google,Pixel 3a,6 +Samsung,Galaxy Fold,2 +Samsung,Galaxy S10,3 +Huawei,Mate 20 Pro,4 +iPhone,XR,6 +Google,Pixel 3 XL,4 +Google,Pixel 3,4 +iPhone,XS,6 +LG,G7 Thin Q,4 +OnePlus,6,5 +Huawei,P20 Pro,4 +Samsung,Galaxy S9,4 +Samsung,Galaxy S9 Plus,4 +iPhone,X,6 +Google,Pixel 2 XL,6 +Google,Pixel 2,6 +Huawei,Mate 10 Pro,4 +iPhone,8 Plus,6 +iPhone,8,6 +Samsung,Galaxy Note8,4 +Essential,Phone,1 +Samsung,Galaxy Note Fan Edition,4 +OnePlus,5,7 +Samsung,Galaxy S8,4 +Samsung,Galaxy S8 Plus,4 +LG,G6,5 +Huawei,Mate 9,5 +Google,Pixel,7 +Google,Pixel XL,7 +iPhone,7,7 +iPhone,7 Plus,7 +Sony,Xperia X Compact,6 +Moto,Z,7 +Samsung,Galaxy Note7,4 +Asus,Zenfone 3 Max,6 +Meizu,MX6,7 +LG,G5,8 +Vivo,X7 Plus,7 +Vivo,X7,6 +Huawei,P9,7 +Xiaomi,Mi 5,6 +iPhone,SE,6 +Oppo,R9m,7 +Samsung,Galaxy S7 Edge,3 +Samsung,Galaxy S7,3 +Lenovo,K5 Note,6 +Fairphone,2,10 +Huawei,Mate 8,6 +Xiaomi,Redmi Note 3,8 +Wiko,Pulp 4G Phone,7 +iPhone,6s,7 +iPhone,6s Plus,7 +Nexus,5X,7 +Nexus,6P,2 +OnePlus,2,7 +Shift,5.1,6 +LG,G4,8 +Samsung,Galaxy S6,4 +Samsung,Galaxy S6 Edge,3 +HTC,One M9,2 +Nexus,6,7 +iPhone,6,7 +iPhone,6 Plus,7 +Samsung,Galaxy Alpha,5 +Amazon,Fire Phone,3 +Samsung,Galaxy S5 Mini,5 +OnePlus,One,5 +Samsung,Galaxy S5,5 +HTC,One M8,2 +Fairphone,1,7 +Nexus,5,8 +iPhone,5s,6 +iPhone,5c,6 +Motorola,Moto X 1st Generation,7 +Samsung,Galaxy S4,8 +HTC,One,1 +BlackBerry,Z10,8 +Nexus,4,7 +Samsung,Galaxy Note II,8 +iPhone,5,7 +Samsung,Galaxy S III,8 +Motorola,Droid 4,4 +Samsung,Galaxy Nexus,7 +Motorola,Droid RAZR,4 +iPhone,4S,6 +Samsung,Galaxy Note,8 +Motorola,Droid Bionic,9 +Motorola,Droid 3,6 +Samsung,Galaxy S II,8 +Samsung,Galaxy S 4G,6 +iPhone,4 Verizon,6 +Motorola,Atrix 4G,9 +Nexus,S,7 +HTC,Surround,5 +Nokia,N8,8 +iPhone,3GS,7 +iPhone,3G,7 +iPhone,1st Generation,2 diff --git a/scraper.py b/scraper.py index 9f0f1ac..c868b41 100644 --- a/scraper.py +++ b/scraper.py @@ -2,6 +2,7 @@ import time from bs4 import BeautifulSoup import requests import re +import csv def get_device_manufacturers(device_names): list_of_manufacturers_without_tags = [] @@ -38,7 +39,19 @@ def data_from_each(list_of_manufacturers_without_tags, list_of_models_without_ta print(list_of_manufacturers_without_tags[j], list_of_models_without_tags[j], list_of_scores_without_tag[j]) j = j + 1 +def insert_into_csv(list_of_manufacturers_without_tags, list_of_models_without_tags, list_of_scores_without_tag): + j = 0 + for i in list_of_manufacturers_without_tags: + with open('list.csv', 'a', newline='') as file: + writer = csv.writer(file) + print(list_of_manufacturers_without_tags[j], list_of_models_without_tags[j], list_of_scores_without_tag[j]) + writer.writerow([list_of_manufacturers_without_tags[j], list_of_models_without_tags[j], list_of_scores_without_tag[j]]) + j = j + 1 + def main(): + with open('list.csv', 'w', newline='') as file: + writer = csv.writer(file) + writer.writerow(["Manufacturer", "Model", "Score"]) link = "https://www.ifixit.com/smartphone-repairability" page = requests.get(link) soup = BeautifulSoup(page.content, 'html.parser') @@ -47,7 +60,7 @@ def main(): list_of_manufacturers_without_tags = get_device_manufacturers(device_names) list_of_models_without_tags = get_device_models(device_names, soup) list_of_scores_without_tag = format_device_scores(device_scores, soup) - data_from_each(list_of_manufacturers_without_tags, list_of_models_without_tags, list_of_scores_without_tag) + insert_into_csv(list_of_manufacturers_without_tags, list_of_models_without_tags, list_of_scores_without_tag) if __name__ == '__main__': main()