Guest User

bs4_request_7

a guest
Mar 27th, 2020
141
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. r = requests.get(url)
  6.  
  7. soup = BeautifulSoup(r.content)
  8.  
  9. g_data1=soup.find_all("div",{"class":"views-field-nothing-1"})
  10. g_data2=soup.find_all("div",{"class":"views-field-nothing"})
  11.  
  12.  
  13. for item in g_data1:
  14. try:
  15. print item.contents[1].find_all("div",{"class":"views-field-counter"})[0].text
  16. except:
  17. pass
  18. try:
  19. print item.contents[1].find_all("div",{"class":"views-field-course-type"})[0].text
  20. except:
  21. pass
  22.  
  23. for item in g_data2:
  24. try:
  25. print item.contents[1].find_all("div",{"class":"views-field-title"})[0].text
  26. except:
  27. pass
  28. try:
  29. print item.contents[1].find_all("div",{"class":"views-field-address"})[0].text
  30. except:
  31. pass
  32. try:
  33. print item.contents[1].find_all("div",{"class":"views-field-city-state-zip"})[0].text
  34. except:
  35. pass
  36.  
  37. output:
  38.  
  39. /home/martin/dev/python/pga_golf_course.py:7: UserWarning: No parser was explicitly specified,
  40. so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem,
  41. but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
  42.  
  43. The code that caused this warning is on line 7 of the file /home/martin/dev/python/pga_golf_course.py.
  44. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.
  45. soup = BeautifulSoup(r.content)
  46. [Finished in 0.508s]
RAW Paste Data