am a little stuck. The program is supposed to open a website and read save it in a file. Then it is supposd to read everything up til it finds a string and delete everything before it and save it again in a new file. But when i run it i get the first file with the html in and the second file i am trying to make turns out to be blank. Anyone that can point me in the right direction?
import fileinput
import re
import requests
import sys
#linkToGet=sys.argv[1] //Hvordan hente link fra terminalen
#r = requests.get(linkToGet)
#nameOfFile=sys.argv[2]
#Hent nettsiden og lagre kildekoden som en textfil
r = requests.get('https://www.bibel.no/Nettbibelen?query=ud8MMrJeKwHNJdqN05oJoRgo89+A24MHmKzQYWJRSygk2+FVqgPK3UvcYb+xB3j7') #Bare sånn jeg kan builde enkelt fra Atom
print (r.text)
f= open("kap3.txt","w+")
f.write(r.text)
f.close
#Fjern all tekst frem til en linje
TAG = """<A HREF="/Nettbibelen?query=ud8MMrJeKwHNJdqN05oJoc7CfBH5MjZKa4lw+sXwPrCzmbEZmCUXfQz2ApCFmHAq" class='versechapter'>50</A> """
tag_found = False
with open('kap3.txt') as in_file:
with open('kap3ren.txt', 'w') as out_file:
for line in in_file:
if not tag_found:
if line.strip() == TAG:
tag_found = True
else:
out_file.write(line)
It looks like you are only calling out_file.write(line)
if you HAVE found the line you are looking for, your else satement should be indented to be for the inner if.
for line in in_file:
if not tag_found:
if line.strip() == TAG:
tag_found = True
else:
out_file.write(line)
Of course this makes the outer if basically useless so it can be simplified to this:
for line in in_file:
if line.strip() == TAG:
# you're done here so you can break the loop
break
else:
out_file.write(line)