Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating Data after web scraping using python and beautiful soup4

I am trying to scrape data from the Garmin site for golf. I would want to get the name of the golf course and the address but I after running the script. I have noticed that my codes just repeats the first page data over and over again. I also noticed that the page numbers on the website do not start at 1 but at 10 for the second page. How do I go about extracting data from this website and getting all and instead of a repeat of just the first page.

import csv
import codecs
import requests 
from bs4 import BeautifulSoup


courses_list= []
for i in range(10):
    url = "http://sites.garmin.com/clsearch/courses?browse=1&country=US&lang=en&per_page={}".format(i)
    r = requests.get(url)

    soup = BeautifulSoup(r.content)

    g_data2=soup.find_all("div",{"class":"result"})

    for item in g_data2:
     try:
        name= item.contents[3].find_all("div",{"class":"name"})[0].text
        print name
     except:
        name=''
    try:
        address= item.contents[3].find_all("div",{"class":"location"})[0].text
    except:
        address=''


    course=[name,address]
    courses_list.append(course)


with open ('G_Final.csv','a') as file:
    writer=csv.writer(file)
    for row in courses_list:
        writer.writerow([s.encode("utf-8") for s in row])
like image 602
Gonzalo68 Avatar asked Aug 06 '26 13:08

Gonzalo68


1 Answers

You discovered the problem.

Then change

url = "http://...?browse=1&country=US&lang=en&per_page={}".format(i)

to

url = "http://...?browse=1&country=US&lang=en&per_page={}".format(i*20)
like image 115
fferri Avatar answered Aug 09 '26 03:08

fferri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!