Apr-02-2023, 04:15 PM
(This post was last modified: Apr-02-2023, 04:15 PM by BlackHeart.)
Thank you! That helped a lot, and that was such a great tip. I actually got it to work here is my code:
import requests from bs4 import BeautifulSoup from urllib.parse import urljoin # Scrape the links to each monthly results page url = "http://www.calotteryx.com/Fantasy-5/drawing-results-calendar.htm" response = requests.get(url) content = response.content soup = BeautifulSoup(content, 'html.parser') links = [] Base_url = 'http://www.calotteryx.com' for link in soup.find_all('a', class_='noline'): if 'Fantasy' in link.get('href'): links.append(f"{Base_url}{link.get('href')}") # Scrape the winning numbers for each monthly results page winning_numbers = [] for link in links: response = requests.get(link) content = response.content soup = BeautifulSoup(content, 'html.parser') for tag in soup.find_all('div', class_='ball blue5 fcblack1'): numbers = tag.text.strip().split() winning_numbers.append(numbers) # Write the winning numbers to a file with open('winning_numbers.txt', 'w') as Nums: for winners in winning_numbers: Nums.write("%s\n" % winners) print('Done')The last thing I need to figure out is how to format the data that's been scraped. Currently every digit (01,15,02,10, etc) is considered it's own string I think? So it's being written to the document via
Nums.write("%s\n" % winners)and it's format is vertical. After every string it creates a new line, which I think I understand why. It's the %s\n, but I want it to create a new line every 5, or I guess it would be every 10 numbers because each string has two digits in it. My solution was to try something like
Nums.write('%s %s %s %s %s\n' % winners)but I get a TypeError: not enough arguments in string.
I appreciate being pointed in the right direction please. I'm thinking may possibly use f-strings, or format(), but am unsure if those will work like how I want them to.
anyway thanks for all the help.