2

So, as an assignment from Thenewboston, I'm trying to grab a block of code from his site and write it to a file. The code grabbing part works just fine, but the writing part doesn't work:

import requests
from bs4 import BeautifulSoup


def crawler(url):
    source = requests.get(url)
    source_text = source.text
    soup_obj = BeautifulSoup(source_text, "html.parser")
    for code in soup_obj.find('code'):
        codes = str(code)
        final_code = codes.replace('<br/>', '').replace('Â', '')
        print(final_code)
        fx = open('crawler_code.txt', 'w')
        fx.write(final_code)
        fx.close()

crawler('https://thenewboston.com/forum/topic.php?id=1610')
SirDarknight
  • 83
  • 1
  • 2
  • 11

2 Answers2

2

You're overwriting the file within the loop

Open the file for writing before you loop and write

with open('crawler_code.txt', 'w') as fx:
    for code in soup_obj.find('code'):
        codes = str(code)
        final_code = codes.replace('<br/>', '').replace('Â', '')
        print(final_code)
        fx.write(final_code)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

You did not really mentioned what was the issue, but I think as you open the file for each line you're grabbing (therefore erasing the content with 'w' mode), you only have one line written in the file at the end. Is it your problem ?

If so, try to open the file before your loop, at the beginning of the crawl method.

Elliot
  • 308
  • 1
  • 8