mercredi 28 février 2018

How to Save scrapped data to a CSV file in python

This is My Web scrapping code that uses beautiful soup module.. i need the save the data that is getting printed as a csv files

import csv
import requests
from bs4 import BeautifulSoup
for i in range(907):      # Number of pages plus one 
url = "http://www.pga.com/golf-courses/search?page={}&searchbox=Course+Name&searchbox_zip=ZIP&distance=50&price_range=0&course_type=both&has_events=0".format(i)
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")

g_data1=soup.find_all("div",{"class":"views-field-nothing-1"})
g_data2=soup.find_all("div",{"class":"views-field-nothing"})


for item in g_data1:
     try:
                print item.contents[1].find_all("div",{"class":"views-field-counter"})[0].text
     except:
                pass  
     try:
                print item.contents[1].find_all("div",{"class":"views-field-course-type"})[0].text
     except:
                pass


for item in g_data2:
     try:
            print item.contents[1].find_all("div",{"class":"views-field-title"})[0].text
     except:
            pass
     try:
            print item.contents[1].find_all("div",{"class":"views-field-address"})[0].text
     except:
            pass
     try:
            print item.contents[1].find_all("div",{"class":"views-field-city-state-zip"})[0].text
     except:
            pass




Aucun commentaire:

Enregistrer un commentaire