-1

I want to add onto this program to save each crashPoint to a text file, with a new crash point being added onto a new line. I've attempted to do it from past work, but I can't seem to get it to work together.

#Imports
from bs4 import BeautifulSoup
from urllib import urlopen
import time

#Required Fields
pageCount = 1287528


#Loop
while(pageCount>0):

    time.sleep(1)
    html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
    soup = BeautifulSoup(html, "html.parser")

    try:
        section = soup.find('div', {"class":"row panel radius"})
        crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
    except:
        continue
    print(crashPoint[0:-1])
    pageCount+=1

Could someone point out what I'm doing wrong and how to fix it?

Ben Parry
  • 133
  • 1
  • 4
  • 19

4 Answers4

0

I haven't worked with some of the exact modules you're using so unless they do something weird I can't gleam from this. The problems I can see are...

  1. It seems like you have an infinite loop, while pageCount > 0 and pageCount+=1 so this could be an issue
  2. You're printing to the console not to a text file look Code Academy has a great tutorial on working with I/O to teach you this.

I think if you fix the infinite loop and simply work with a text file instead of the console you'll have no problem.

Ceddrick
  • 158
  • 1
  • 3
0

Doing this is pretty straightforward if you just open the output file in append mode:

#Loop
logFile = open("logFile.txt", "a")
while(pageCount>0):

    time.sleep(1)
    html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
    soup = BeautifulSoup(html, "html.parser")

    try:
        section = soup.find('div', {"class":"row panel radius"})
        crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
        logFile.write(crashPoint+"\n")
    except:
        continue
    print(crashPoint[0:-1])
    pageCount+=1
logFile.close()
Natsukane
  • 681
  • 1
  • 8
  • 19
0

Write the data into the file by opening it in append mode. If you are iterating through the file loop, just open the file once and keep writing the new data.

with open("test.txt", "a") as myfile:
    myfile.write(crashPoint[0:-1])

Here are different methods to append data in file using python.

Community
  • 1
  • 1
bhansa
  • 7,282
  • 3
  • 30
  • 55
-1

to print to text file

from bs4 import BeautifulSoup
from urllib import urlopen
import time

#Required Fields
pageCount = 1287528

fp = open ("logs.txt","w")
#Loop
while(pageCount>0):

    time.sleep(1)
    html = urlopen('https://www.csgocrash.com/game/1/%s' %(pageCount)).read()
   soup = BeautifulSoup(html, "html.parser")

   try:
       section = soup.find('div', {"class":"row panel radius"})
       crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
   except:
       continue
   print(crashPoint[0:-1])
   #write to file here
   fp.write(crashPoint[0:-1]+'\n')
   #i think its minus
   pageCount-=1
fp.close()
NishanthSpShetty
  • 661
  • 5
  • 14