I'm new to python web scraping, and I was attempting to take the addresses of different winmar locations in Canada, as well as put the results into a csv file. So far, the only way I have found to differentiate between the different locations' sites is by a code at the end of the address (numbers). The problem is that the results do not change as the program runs, and instead produces the results of the first location (305) when printing and into the csv file. Thanks for your time and consideration!
Here's my code:
import csv
import requests
from bs4 import BeautifulSoup
x = 0
numbers = ['305', '405', '306', '307', '308', '309', '4273']
f = csv.writer(open('Winmar_locations.csv', 'w'))
f.writerow(['City:', 'Address:'])
for links in numbers:
for x in range(0, 6):
url = 'https://www.winmar.ca/find-a-location/' + str(numbers[x])
r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")
location_name = soup.find("div", attrs={"class": "title_block"})
location_name_items = location_name.find_all('h2')
location_list = soup.find(class_='quick_info')
location_list_items = location_list.find_all('p')
for name in location_name_items:
names = name.text
names = names.replace('Location | ', '')
for location in location_list_items:
locations = location.text.strip()
locations = locations.replace('24 Hour Emergency | (902) 679-1116','')
print(names, locations)
x = x+1
f.writerow([names, locations])