Search code examples
pythonbeautifulsoupxlrd

Python not printing beautifulsoup data to .txt file (or I can't find it)


I'm trying to put all the anchor text on a page in a txt file

print(anchor.get('href'))
with open('file.txt', 'a') as fd:
    fd.write(anchor.get('href') + '\n')

and the script executes with no errors but I cannot find file.txt anywhere on my computer. Am I missing out on something really obvious?


Solution

  • With file open mode a (append) the file will be created if it doesn't already exist, otherwise writes will be appended to the end of the file. As written in the question, the file will be created in the current directory of the running process... look there.

    from bs4 import BeautifulSoup
    import requests
    
    response = requests.get('http://httpbin.org/')
    soup = BeautifulSoup(response.text)
    with open('file.txt', 'a') as fd:
        links = (link.get('href') for link in soup.find_all('a'))
        fd.write('\n'.join(links) + '\n')