写入 txt 文件多个字符串,只保存最后一个字符串?

Writing to txt file multiple string, only last string saves?

我知道这是一个重复的问题,但是从网络上的所有答案中我找不到解决方案,因为所有问题都抛出错误。 只是想从网上抓取 headers 并将它们保存到 txt 文件中。 抓取代码效果很好,但是,它只保存最后一个字符串,绕过所有 headers 到最后一个。 我试过循环,在抓取之前写代码,附加到列表等,不同的抓取方法但是都有同样的问题。 请帮忙

这是我的代码

def nytscrap():
    from bs4 import BeautifulSoup
    import requests

url = "http://www.nytimes.com"

page = BeautifulSoup(requests.get(url).text, "lxml")

for headlines in page.find_all("h2"):
    print(headlines.text.strip())

filename = "NYTHeads.txt" 
with open(filename, 'w') as file_object:
        file_object.write(str(headlines.text.strip()))

'''

每次 for 循环运行时,它都会覆盖 headlines 变量,因此当您开始写入文件时,headlines 变量仅存储最后一个标题。一个简单的解决方案是将 for 循环放入 with 语句中,如下所示:

with open(filename, 'w') as file_object:
    for headlines in page.find_all("h2"):
        print(headlines.text.strip())
        file_object.write(headlines.text.strip()+"\n") # write a newline after each headline

这里是根据建议更正的完整工作代码。

    from bs4 import BeautifulSoup
import requests

def nytscrap():
    from bs4 import BeautifulSoup
    import requests

url = "http://www.nytimes.com"

page = BeautifulSoup(requests.get(url).text, "lxml")
filename = "NYTHeads.txt" 
with open(filename, 'w') as file_object:
    for headlines in page.find_all("h2"):
        print(headlines.text.strip())
        file_object.write(headlines.text.strip()+"\n")

此代码在 Jupiter 工作时会出现错误,但是当 打开文件,但是当文件在 Jupiter 之外打开时 headers 已保存...