SHARE
TWEET

bs4_request_3

a guest Mar 27th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. from urllib.request import urlopen
  4. from bs4 import BeautifulSoup
  5.  
  6. file = "Details.csv"
  7. f = open(file, "w")
  8. Headers = "Name,Address,City,Phone,Website\n"
  9. f.write(Headers)
  10. for page in range(1,5):
  11.     url = "http://www.pga.com/golf-courses/search?page={}&searchbox=Course%20Name&searchbox_zip=ZIP&distance=50&price_range=0&course_type=both&has_events=0".format(page)
  12.     html = urlopen(url)
  13.     soup = BeautifulSoup(html,"html.parser")
  14.     Title = soup.find_all("div", {"class":"views-field-nothing"})
  15.     for i in Title:
  16.         try:
  17.             name = i.find("div", {"class":"views-field-title"}).get_text()
  18.             address = i.find("div", {"class":"views-field-address"}).get_text()
  19.             city = i.find("div", {"class":"views-field-city-state-zip"}).get_text()
  20.             phone = i.find("div", {"class":"views-field-work-phone"}).get_text()
  21.             website = i.find("div", {"class":"views-field-website"}).get_text()
  22.             print(name, address, city, phone, website)
  23.             f.write("{}".format(name).replace(",","|")+ ",{}".format(address)+ ",{}".format(city).replace(",", " ")+ ",{}".format(phone) + ",{}".format(website) + "\n")
  24.         except: AttributeError
  25. f.close()
  26.  
  27.  
  28. output:
  29.  
  30. Traceback (most recent call last):
  31.   File "/home/martin/dev/python/yahoo_finance.py", line 1, in <module>
  32.     from urllib.request import urlopen
  33. ImportError: No module named request
  34. [Finished in 37.273s]
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
Top