报废为CSV的数据未完整/有序

时间:2018-11-06 20:08:11

标签: python beautifulsoup python-requests export-to-csv

import csv
import requests
from bs4 import BeautifulSoup

for i in range(1, 5):
    url = "https://www.medplusmedicalsupply.com/exam-and-diagnostic?p="+str(i)+"&product_list_limit=25"
    html = requests.get(url).text
    soup = BeautifulSoup(html, "html.parser")
    products = soup.findAll('div', {"class": "product details product-item-details"})
    all_product = []
    print(url)

    for product in products:
        product_details = dict()
        product_details['name'] = product.find('a').text.strip('\n\r\t": ').strip('\n\r\t": ').strip('\n\r\t": ').strip('\n\r\t": ')
        product_details['brand'] = product.find('div', {'class': 'value'}).text.strip('\n\r\t": ').strip('\n\r\t": ').strip('\n\r\t": ')
        product_details['packaging'] = product.find('div', {'class': 'pack'}).text.strip('\n\r\t": ').strip('\n\r\t": ').strip('\n\r\t": ')
        product_details['availability'] = product.find('div', {'class': 'avail pack'}).text.strip('\n\r\t": ').strip('\n\r\t": ').strip('\n\r\t": ')
        product_details['price'] = product.find('span', {'class': 'price'}).text.strip('\n\r\t": ').strip('\n\r\t": ').strip('\n\r\t": ')
        product_details['packaging'] = product_details['packaging'][9:] # here we're cutting redundant part of string "Brand: \n\n"
        product_details['availability'] = product_details['availability'][16:] # here we're cutting redundant part of string "Availability: \n\n"
        all_product.append(product_details)

    print(all_product)

with open('prod.csv', 'w+') as csvFile:
    writer = csv.writer(csvFile)
    writerow = (['Name', 'Brand', 'Packaging', 'Availability', 'Price'])
    for product in all_product:
        writer.writerow([product['name'], product['brand'],product['packaging'], product['availability'], product['price']])

这是python中的输出:

https://www.medplusmedicalsupply.com/exam-and-diagnostic?p=1&product_list_limit=25
[{'name': 'Kimberly Clark Blue Nitrile Exam Gloves -100/BX', 'brand': 'Kimberly Clark', 'packaging': 'Kimberly Clark', 'availability': '1-3 Days', 'price': '$26.94'}, {'name': 'Pro Advantage Gloves - 200/BX', 'brand': 'Pro Advantage', 'packaging': 'Pro Advantage', 'availability': '1-3 Days', 'price': '$28.94'}, {'name': 'Kimberly Clark Powder Free Exam Gloves - 500/CS', 'brand': 'Kimberly Clark', 'packaging': 'Kimberly Clark', 'availability': '1-3 Days', 'price': '$136.99'}, {'name': 'Kimberly Clark Sterile Pairs Exam Gloves - 400/CS', 'brand': 'Kimberly Clark', 'packaging': 'Kimberly Clark', 'availability': '1-3 Days', 'price': '$205.99'}, {'name': 'Dynarex Multi Care Vinyl Exam Gloves Non-Latex - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$61.99'}, {'name': 'Dynarex Tillotson Powder Free Plus Latex Exam Gloves - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$109.99'}, {'name': 'Dynarex 6512 True Advantage High Risk Nitrile Exam Gloves, 8 mil - 500/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$105.99'}, {'name': 'Dynarex 6512 True Advantage High Risk Nitrile Exam Gloves, 8 mil - 50/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$20.99'}, {'name': 'Dynarex Tillotson Tru Advantage PF Nitrile Exam Gloves - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$101.99'}, {'name': 'Dynarex Tillotson Tru Advantage PF Nitrile Exam Gloves - 100/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$18.99'}, {'name': 'Dynarex Tillotson Dental Care Powderless Inner Surface Latex Exam Gloves - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$110.99'}, {'name': 'Dynarex Tillotson Dental Care Powderless Inner Surface Latex Exam Gloves - 100/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$21.99'}, {'name': 'Dynarex Ultra Care Powderless Inner Surface Latex Exam Gloves - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$110.99'}, {'name': 'Dynarex Ultra Care Powderless Inner Surface Latex Exam Gloves - 100/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$21.99'}, {'name': 'Dynarex DynaPlus Nitrile Exam Gloves - 2000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$125.99'}, {'name': 'Dynarex DynaPlus Nitrile Exam Gloves - 200/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$24.99'}, {'name': 'Dynarex Black Nitrile Exam Gloves (non-latex) Powder Free - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$89.99'}, {'name': 'Dynarex Black Nitrile Exam Gloves (non-latex) Powder Free - 100/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$17.99'}, {'name': 'Dynarex Sterile Latex Exam Gloves Powder Free - (Pairs) - 400 PR/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$201.99'}, {'name': 'Dynarex Sterile Latex Exam Gloves Powder Free - (Pairs) - 50 PR/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$41.99'}, {'name': 'Dynarex High Risk Latex Exam Gloves, 15mil Blue Powder Free - 500/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$131.99'}, {'name': 'Dynarex High Risk Latex Exam Gloves, 15mil Blue Powder Free - 50/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$25.00'}, {'name': 'Dynarex High Risk Latex Exam Gloves, 10mil White Powder Free - 500/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$120.99'}, {'name': 'Dynarex High Risk Latex Exam Gloves, 10mil White Powder Free - 50/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$23.99'}, {'name': 'Dynarex Black Arrow Latex Exam Gloves - 1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$104.99'}]
https://www.medplusmedicalsupply.com/exam-and-diagnostic?p=2&product_list_limit=25
[{'name': 'Dynarex Black Arrow Latex Exam Gloves - 100/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$19.99'}, {'name': 'Dynarex Safe-Touch Vinyl Exam Gloves Powder Free -1000/CS', 'brand': 'Safetouch', 'packaging': 'Safetouch', 'availability': '1-3 Days', 'price': '$61.99'}, {'name': 'Halyard Health Purple Nitrile XTRA PF Extended Cuff - 50/BX', 'brand': 'Safetouch', 'packaging': 'Safetouch', 'availability': '1-3 Days', 'price': '$13.95'}, {'name': 'Dynarex Safe-Touch Vinyl Exam Gloves Powder Free -100/BX', 'brand': 'Safetouch', 'packaging': 'Safetouch', 'availability': '1-3 Days', 'price': '$13.99'}, {'name': 'Pro Advantage Vinyl Exam Glove, Powder Free - 100/BX', 'brand': 'Pro Advantage', 'packaging': 'Pro Advantage', 'availability': '1-3 Days', 'price': '$14.94'}, {'name': 'BSN Medical Therall Arthritis Gloves SM -1/PR', 'brand': 'BSN Medical', 'packaging': 'BSN Medical', 'availability': '1-3 Days', 'price': '$37.94'}, {'name': 'Kimberly Clark Exam Glove, PF Nitrile - 200 PR/CS', 'brand': 'Kimberly Clark', 'packaging': 'Kimberly Clark', 'availability': '1-3 Days', 'price': '$181.99'}, {'name': 'Dynarex Safe-Touch Latex Exam Gloves Powder Free -1000/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$89.99'}, {'name': 'Dynarex Safe-Touch Powder Free Latex Exam Gloves - 100/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$17.99'}, {'name': 'Molnlycke Health Care Biogel Surgeons Glove ST Latex P/F - 50/BX', 'brand': 'Biogel', 'packaging': 'Biogel', 'availability': '1-3 Days', 'price': '$119.95'}, {'name': 'Spectra 360 Electrode Gel - 60 Gram Tube', 'brand': 'Biogel', 'packaging': 'Biogel', 'availability': '199 in stock', 'price': '$1.99'}, {'name': 'Latex Surgeons Gloves Powder Free – Sterile Pairs - 200/CS', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$131.99'}, {'name': 'Latex Surgeons Gloves Powder Free – Sterile Pairs - 50/BX', 'brand': 'Dynarex', 'packaging': 'Dynarex', 'availability': '1-3 Days', 'price': '$47.99'}, {'name': 'Welch Allyn 97250 3.5 V Diagnostic Set with Coaxial Opthalmoscope / Diagnostic Otoscope Handle and Hard Case', 'brand': 'Welch Allyn', 'packaging': 'Welch Allyn', 'availability': '831 in stock', 'price': '$399.00'}, {'name': 'Chattanooga 4004 Stainless Steel Mobile Cart - 3 Shelves - 1/EA', 'brand': 'Chattanooga', 'packaging': 'Chattanooga', 'availability': '1 in stock', 'price': '$288.92'}, {'name': 'Chattanooga 4018 Stainless Steel Mobile Cart - 2 Shelves with Drawer - 1/EA', 'brand': 'Chattanooga', 'packaging': 'Chattanooga', 'availability': '1-3 Days', 'price': '$474.95'}, {'name': 'Chattanooga 630309-001 Saunders Education Manuals- Managing Back Pain -1/EA', 'brand': 'Saunders', 'packaging': 'Saunders', 'availability': '1-3 Days', 'price': '$5.95'}, {'name': 'Chattanooga Pnematic Stool with Back - 1/EA', 'brand': 'Chattanooga', 'packaging': 'Chattanooga', 'availability': '1-3 Days', 'price': '$149.95'}, {'name': 'Chattanooga 77062 Pnematic Stool without Back - Dove Gray - 1/EA', 'brand': 'Chattanooga', 'packaging': 'Chattanooga', 'availability': '4 in stock', 'price': '$129.95'}, {'name': 'Chattanooga 7706 Pnematic Stool without Back - Burgundy - 1/EA', 'brand': 'Chattanooga', 'packaging': 'Chattanooga', 'availability': '78 in stock', 'price': '$129.00'}, {'name': 'Welch Allyn 08800-U 4.6 V Halogen Lamp for KleenSpec Vaginal Specula Illumination Systems (#78812, #78814, #78816)', 'brand': 'Welch Allyn', 'packaging': 'Welch Allyn', 'availability': '1-3 Days', 'price': '$28.00'}, {'name': 'Halyard Health Fluidshield Fog Free Procedure Mask - Orange - Box of 40', 'brand': 'Welch Allyn', 'packaging': 'Welch Allyn', 'availability': '66 in stock', 'price': '$15.99'}, {'name': 'Welch Allyn 45008-0000 Welch Allyn Silver/Silver Chloride Resting Tab ECG Electrode - 1000/BX', 'brand': 'Welch Allyn', 'packaging': 'Welch Allyn', 'availability': '14 in stock', 'price': '$119.95'}, {'name': 'Welch Allyn 45008-0000 Welch Allyn Silver/Silver Chloride Resting Tab ECG Electrode - 100/PK', 'brand': 'Welch Allyn', 'packaging': 'Welch Allyn', 'availability': '141 in stock', 'price': '$13.95'}, {'name': 'Vermed A10009-100F Silverest Foam Resting Tab ECG Electrode - 100/PK', 'brand': 'Welch Allyn', 'packaging': 'Welch Allyn', 'availability': '5-10 Days', 'price': '$21.95'}]





https://www.medplusmedicalsupply.com/exam-and-diagnostic?p=3&product_list_limit=25
[{'name': 'Nikomed 0815 Nikotab Foam Resting Tab ECG Electrode Pediatric - 5000/CS', 'brand': '5000/CS', 'packaging': ': \n5000/CS', 'availability': '5-10 Days', 'price': '$189.95'}, {'name': 'Nikomed 0815 Nikotab Foam Resting Tab ECG Electrode Pediatric - 500/BX', 'brand': '500/BX', 'packaging': ': \n500/BX', 'availability': '5-10 Days', 'price': '$24.95'}, {'name': 'Nikomed 0515 Nikotab Foam Resting Tab ECG Electrode - 500/BX', 'brand': '500/BX', 'packaging': ': \n500/BX', 'availability': '5-10 Days', 'price': '$29.95'}, {'name': 'Nikomed 0515 Nikotab Foam Resting Tab ECG Electrode - 100/PK', 'brand': '100/PK', 'packaging': ': \n100/PK', 'availability': '5-10 Days', 'price': '$11.95'}, {'name': 'Nikomed 0315 Nikotab Foam Resting Tab ECG Electrode - 5000/CS', 'brand': '5000/CS', 'packaging': ': \n5000/CS', 'availability': '37 in stock', 'price': '$199.95'}, {'name': 'Nikomed 0315 Nikotab Foam Resting Tab ECG Electrode - 500/BX', 'brand': '500/BX', 'packaging': ': \n500/BX', 'availability': '379 in stock', 'price': '$22.95'}, {'name': 'Nikomed 0315 Nikotab Foam Resting Tab ECG Electrode - 100/PK', 'brand': '100/PK', 'packaging': ': \n100/PK', 'availability': '37 in stock', 'price': '$9.95'}, {'name': 'Nikomed 5533-5 Trace 1 Foam Monitoring ECG Electrode - 300/BX', 'brand': '300/BX', 'packaging': ': \n300/BX', 'availability': '2 in stock', 'price': '$62.95'}, {'name': 'Medline MDS616101A Medline Foam Resting Tab ECG Electrode - 5000/CS', 'brand': 'Medline', 'packaging': 'Medline', 'availability': '5-10 Days', 'price': '$199.95'}, {'name': 'Medline MDS616101AZ Medline Foam Resting Tab ECG Electrode - 500/BX', 'brand': 'Medline', 'packaging': 'Medline', 'availability': '5-10 Days', 'price': '$22.95'}, {'name': 'Covidien ES40281 Soft-E Cloth Monitoring ECG Electrode Pediatric - 450/CS', 'brand': 'Covidien, Soft-E', 'packaging': 'Covidien, Soft-E', 'availability': '1 in stock', 'price': '$154.95'}, {'name': 'Covidien ES40281 Soft-E Cloth Monitoring ECG Electrode Pediatric - 30/PK', 'brand': 'Covidien, Soft-E', 'packaging': 'Covidien, Soft-E', 'availability': '15 in stock', 'price': '$16.95'}, {'name': 'Covidien 22450 Medi-Trace 450 Series Foam Monitoring Electrodes - 50/PK', 'brand': 'Covidien, Kendall, Medi-Trace', 'packaging': 'Covidien, Kendall, Medi-Trace', 'availability': '5-10 Days', 'price': '$11.95'}, {'name': 'Covidien 30807732 Q-Trace Resting Tab ECG Electrode with Aggressive Adhesive - 2000/CS', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '5-10 Days', 'price': '$90.00'}, {'name': 'Covidien 30807732 Q-Trace Resting Tab ECG Electrode with Aggressive Adhesive - 100/PK', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '5-10 Days', 'price': '$6.50'}, {'name': 'Covidien 31447793 Kendall Resting Tab ECG Electrode - 4000/CS', 'brand': 'Covidien, Kendall', 'packaging': 'Covidien, Kendall', 'availability': '5-10 Days', 'price': '$249.95'}, {'name': 'Covidien 31447793 Kendall Resting Tab ECG Electrode - 100/PK 10PK/BX', 'brand': 'Covidien, Kendall', 'packaging': 'Covidien, Kendall', 'availability': '5-10 Days', 'price': '$64.95'}, {'name': 'Covidien 31447793 Kendall Resting Tab ECG Electrode - 100/PK', 'brand': 'Covidien, Kendall', 'packaging': 'Covidien, Kendall', 'availability': '5-10 Days', 'price': '$7.95'}, {'name': 'Covidien Ef00149 Kendall Foam Resting Tab ECG Electrode - 4000/CS', 'brand': 'Cleartrace 2, Conmed', 'packaging': 'Cleartrace 2, Conmed', 'availability': '5 in stock', 'price': '$169.95'}, {'name': 'Conmed 1700-005 Cleartrace Tape Monitoring ECG Electrode 5" x 10" - 600/CS', 'brand': 'Cleartrace, Conmed', 'packaging': 'Cleartrace, Conmed', 'availability': '1 in stock', 'price': '$149.95'}, {'name': 'Conmed 1700-005 Cleartrace Tape Monitoring ECG Electrode 5" x 10" - 50/BX', 'brand': 'Cleartrace, Conmed', 'packaging': 'Cleartrace, Conmed', 'availability': '5-10 Days', 'price': '$12.95'}, {'name': 'Conmed 1700-003 Cleartrace Tape Monitoring ECG Electrode 3" x 10" - 600/CS', 'brand': 'Cleartrace, Conmed', 'packaging': 'Cleartrace, Conmed', 'availability': '3-5 Days', 'price': '$159.95'}, {'name': 'Conmed 1700-003 Cleartrace Tape Monitoring ECG Electrode 3" x 10" - 30/BX', 'brand': 'Cleartrace, Conmed', 'packaging': 'Cleartrace, Conmed', 'availability': '3-5 Days', 'price': '$10.50'}, {'name': 'Burdick 015-0630-00 Blue Max Foam Resting Tab ECG Electrode - 100/PK', 'brand': 'Blue Max, Burdick', 'packaging': 'Blue Max, Burdick', 'availability': '3-5 Days', 'price': '$17.50'}, {'name': 'Burdick 047029 CardioSens Ultra II Silver/Silver Chloride Resting Tab ECG Electrode with Aggressive Adhesive - 5000/CS', 'brand': 'Burdick, CardioSens', 'packaging': 'Burdick, CardioSens', 'availability': '1 in stock', 'price': '$479.95'}]
https://www.medplusmedicalsupply.com/exam-and-diagnostic?p=4&product_list_limit=25
[{'name': 'Burdick 047029 CardioSens Ultra II Silver/Silver Chloride Resting Tab ECG Electrode with Aggressive Adhesive - 500/BX', 'brand': 'Burdick, CardioSens', 'packaging': 'Burdick, CardioSens', 'availability': '16 in stock', 'price': '$49.95'}, {'name': 'Bio-Protech T716 Telectrode Foam Resting Tab ECG Electrode - 4000/CS', 'brand': 'Bio-Protech', 'packaging': 'Bio-Protech', 'availability': '12 in stock', 'price': '$390.00'}, {'name': 'Bio-Protech T716 Telectrode Foam Resting Tab ECG Electrode - 1000/BX', 'brand': 'Bio-Protech', 'packaging': ' Bio-Protech', 'availability': '48 in stock', 'price': '$110.00'}, {'name': 'Bio-Protech Pt2334 PRO-TAB Tape Resting ECG Electrodes - 5000/CS', 'brand': 'Bio-Protech', 'packaging': 'Bio-Protech', 'availability': '33 in stock', 'price': '$189.95'}, {'name': '3M 2570-3 Red Dot Foam Monitoring ECG Electrode with Abrader- Pack of 3', 'brand': '3M, Red Dot', 'packaging': '3M, Red Dot', 'availability': '400 in stock', 'price': '$2.99'}, {'name': 'Clarity CT-DTG-PLUS2 HCG Pregnancy Test - 25 Tests - Urine Cassette', 'brand': '3M, Red Dot', 'packaging': '3M, Red Dot', 'availability': '33 in stock', 'price': '$24.99'}, {'name': 'BSN Medical Jobst Medicalwear Glove With Velcro - 1/EA', 'brand': '3M, Red Dot', 'packaging': '3M, Red Dot', 'availability': '5-10 Days', 'price': '$41.60'}, {'name': 'Welch Allyn ProBP 2400 Digital Blood Pressure Device', 'brand': '3M, Red Dot', 'packaging': '3M, Red Dot', 'availability': '5-10 Days', 'price': '$430.00'}, {'name': 'Covidien 31433538 5400 Diagnostic Tab ECG Electrodes- Case of 4000', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '5-10 Days', 'price': '$160.00'}, {'name': 'Covidien 31433538 Q-Trace Foam Resting Tab ECG Electrode - 100/PK', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '5-10 Days', 'price': '$48.00'}, {'name': 'Covidien 31433538 Q-Trace Foam Resting Tab ECG Electrode - 100/PK', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '5-10 Days', 'price': '$4.99'}, {'name': 'Unistik 3 Normal Safety Lancets 23G X 1.88mm Yellow - Box of 50', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '73 in stock', 'price': '$9.99'}, {'name': 'SEK-141 Osom Dipstick Strep A Test Kit, 50/BX', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '5-10 Days', 'price': '$89.99'}, {'name': '3M-5639 Littmann Classic III Stethoscope, SS Finish Chestpiece, Pink Tube, 27" ,1/EA', 'brand': 'Covidien, Q-Trace', 'packaging': 'Covidien, Q-Trace', 'availability': '1-3 Days', 'price': '$79.00'}, {'name': '3M-5621 Littmann Classic III Stethoscope, SS Finish Chestpiece, Gray Tube, 27" ,1/EA', 'brand': '3M, Littmann, Littmann Classic III', 'packaging': '3M, Littmann, Littmann Classic III', 'availability': '5-10 Days', 'price': '$79.00'}, {'name': 'Welch Allyn 97150-M 3.5v Diagnostic Set Opthalmoscope / Otoscope with Rechargeable Handle and Hard Case', 'brand': '3M, Littmann, Littmann Classic III', 'packaging': '3M, Littmann, Littmann Classic III', 'availability': '2 in stock', 'price': '$515.00'}, {'name': '3M 5627 Stethoscope, Standard-Finish Chestpiece, Burgundy Tube, 27', 'brand': '3M, Littmann, Littmann Classic III', 'packaging': '3M, Littmann, Littmann Classic III', 'availability': '1 in stock', 'price': '$79.00'}, {'name': 'Welch Allyn 23810-SET 3.5v Macroview Otoscope with Handle and Hard Case', 'brand': '3M, Littmann, Littmann Classic III', 'packaging': '3M, Littmann, Littmann Classic III', 'availability': '6 in stock', 'price': '$265.00'}, {'name': 'Omron 7321-E M6 Comfort Digital Blood Pressure Monitor', 'brand': '3M, Littmann, Littmann Classic III', 'packaging': '3M, Littmann, Littmann Classic III', 'availability': '10 in stock', 'price': '$120.00'}, {'name': 'Welch Allyn 06000-200N Braun Thermoscan Pro 6000 Ear Thermometer with Large Cradle', 'brand': '3M, Littmann, Littmann Classic III', 'packaging': '3M, Littmann, Littmann Classic III', 'availability': '3 in stock', 'price': '$199.00'}, {'name': 'Covidien Ef00149 Kendall Foam Resting Tab ECG Electrode - 1000/BX', 'brand': 'Cleartrace 2, Conmed', 'packaging': 'Cleartrace 2, Conmed', 'availability': '20 in stock', 'price': '$55.00'}, {'name': 'Covidien Ef00149 Kendall Foam Resting Tab ECG Electrode - 100/PK', 'brand': 'Cleartrace 2, Conmed', 'packaging': 'Cleartrace 2, Conmed', 'availability': '208 in stock', 'price': '$7.50'}, {'name': '3M 6158 Littmann Cardiology IV Stethoscope, 27" Raspberry Tubing, 1/EA', 'brand': '3M, Littmann, Littmann Cardiology IV', 'packaging': '3M, Littmann, Littmann Cardiology IV', 'availability': '21 in stock', 'price': '$168.00'}, {'name': '3M 6157 Littmann Cardiology IV Stethoscope, 27" Caribbean Blue Tubing, 1/EA', 'brand': '3M, Littmann, Littmann Cardiology IV', 'packaging': '3M, Littmann, Littmann Cardiology IV', 'availability': '9 in stock', 'price': '$168.00'}, {'name': '3M 6156 Littmann Cardiology IV Stethoscope, 27" Plum Tubing, 1/EA', 'brand': '3M, Littmann, Littmann Cardiology IV', 'packaging': '3M, Littmann, Littmann Cardiology IV', 'availability': '103 in stock', 'price': '$168.00'}]

问题是导出到CSV文件,当我打开“ prod.csv”文件时,它似乎只包括所抓取信息的最后一页(25个项目),没有标题。每个要抓取的页面包含25个项目,共5个页面(总共125个产品)应该在我的csv文件中。只是想知道如何包含所有带有标题的数据。谢谢!

2 个答案:

答案 0 :(得分:0)

问题在于您正在用新数据覆盖密钥,因此可以改为使用namedtuple()

import collections

all_products = collections.namedtuple('Products', ['Name', 'Brand', 'Packaging', 'Availability', 'Price'])

答案 1 :(得分:0)

每次all_product将设置为[],因此all_product仅包含最后一页的结果。您必须在循环外部定义all_product

all_product = []

for i in range(1, 5):
    #code