SHARE
TWEET

Untitled

a guest Aug 25th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import csv
  2. import requests
  3. from bs4 import BeautifulSoup
  4. url = "http://www.pga.com/golf-courses/search?searchbox=Course+Name&searchbox_zip=ZIP&distance=50&price_range=0&course_type=both&has_events=0"
  5.  
  6. r = requests.get(url)
  7.  
  8. soup = BeautifulSoup(r.content)
  9.  
  10. g_data1=soup.find_all("div",{"class":"views-field-nothing-1"})
  11. g_data2=soup.find_all("div",{"class":"views-field-nothing"})
  12.  
  13. courses_list=[]
  14.  
  15. for item in g_data2:
  16.      try:
  17.           name=item.contents[1].find_all("div",{"class":"views-field-title"})[0].text
  18.      except:
  19.           name=''
  20.      try:
  21.           address1=item.contents[1].find_all("div",{"class":"views-field-address"})[0].text
  22.      except:
  23.           address1=''
  24.      try:
  25.           address2=item.contents[1].find_all("div",{"class":"views-field-city-state-zip"})[0].text
  26.      except:
  27.           address2=''
  28.      try:
  29.           website=item.contents[1].find_all("div",{"class":"views-field-website"})[0].text
  30.      except:
  31.           website=''  
  32.      try:
  33.           Phonenumber=item.contents[1].find_all("div",{"class":"views-field-work-phone"})[0].text
  34.      except:
  35.           Phonenumber=''      
  36.  
  37.      course=[name,address1,address2,website,Phonenumber]
  38.      courses_list.append(course)
  39.  
  40.      with open ('filename5.csv','wb') as file:
  41.           writer=csv.writer(file)
  42.           for row in courses_list:
  43.                writer.writerow(row)    
  44.  
  45. #for item in g_data1:
  46.      #try:
  47.           #print item.contents[1].find_all("div",{"class":"views-field-counter"})[0].text
  48.      #except:
  49.           #pass  
  50.      #try:
  51.           #print item.contents[1].find_all("div",{"class":"views-field-course-type"})[0].text
  52.      #except:
  53.           #pass
  54.  
  55. #for item in g_data2:
  56.    #try:
  57.       #print item.contents[1].find_all("div",{"class":"views-field-title"})[0].text
  58.    #except:
  59.       #pass
  60.    #try:
  61.       #print item.contents[1].find_all("div",{"class":"views-field-address"})[0].text
  62.    #except:
  63.       #pass
  64.    #try:
  65.       #print item.contents[1].find_all("div",{"class":"views-field-city-state-zip"})[0].text
  66.    #except:
  67.       #pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top