如何使用 python 在文件中写入多行

how to write multiple lines in a file using python

我知道如何将多行写入一个文件,如果我知道我想写多少行的话。但是,当我想写多行时,问题就来了,但是,我不知道它们会有多少

我正在开发一个应用程序,该应用程序从网站上抓取并将结果的 link 存储在文本文件中。但是,我们不知道它会回复多少行。我的代码现在如下。

r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})
for episode in subtitles:
  x = episode.find_all('a')
  for a in x:
   #print a['href']

   z = a['href']

  l = 'http://www.crunchyroll.com'+ z
  print l

这给了我想要的 output.So,我试图通过添加将内容写入文件:

file = open("BatchLinks.txt", "w")
file.write(l)
file.close()

但是,不幸的是,它只写了第一个 link。我怎样才能添加其他 link 呢?

先打开文件然后边写边写:

 with open(fa, "w") as f:
    r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
    soup = BeautifulSoup(r.text)
    print soup.title
    subtitles = soup.findAll('div', {'class': 'wrapper container-shadow hover-classes'})
    for episode in subtitles:
        x = episode.find_all('a')
        for a in x:
            z = a['href']
            f.write('http://www.crunchyroll.com{}\n'.format(z) )

除非您希望所有 link 都在一行中,否则您需要在加入的 link 末尾添加 \n。您的代码还会写最后一个 link 而不是第一个

输出:

http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-13-happy-days-678059
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-12-baby-skip-beat-678057
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-11-the-weight-of-value-and-the-value-of-weight-678055
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-10-fool-couple-678053
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-9-made-a-wish-it-came-true-and-i-got-in-trouble-678051
.........................

确保在 for 循环中写入 link。使用 with 命令也可以节省您手动关闭文件的时间。 这应该有效:

r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})

with open("BatchLinks.txt","w") as file: 

    for episode in subtitles:
        x = episode.find_all('a')
        for a in x:

            z = a['href']

            link = 'http://www.crunchyroll.com'+ z
            print link
            file.write(link)

https://docs.python.org/3/library/functions.html#open

'w' open for writing, truncating the file first

每次以 'w' 模式打开文件时都会截断它,这意味着丢弃任何现有内容。您可能希望文件保持打开状态,直到您完成写入,然后关闭它。

下面的代码应该允许您将多行写入一个文件。

 with open(fa, "w") as f:
    r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-  husband-is-saying')
    soup = BeautifulSoup(r.text)
    print soup.title
    subtitles = soup.findAll('div', {'class': 'wrapper container-shadow  hover-classes'})
    for episode in subtitles:
        x = episode.find_all('a')
        for a in x:
            z = a['href']
            f.write('http://www.crunchyroll.com{}\n'.format(z) )