簡體   English   中英

如何使用python在文件中寫多行

[英]how to write multiple lines in a file using python

如果知道要寫入多少行,我知道如何向文件寫入多行。 但是,問題出在我想寫多行時,但是,我不知道它們將是多少

我正在開發一個從網站抓取並將結果的鏈接存儲在文本文件中的應用程序。 但是,我們不知道它將回復多少行。 我的代碼如下。

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

這給了我想要的輸出。因此,我嘗試通過添加以下內容在文件中寫入內容:

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

但是,不幸的是,它僅寫入第一個鏈接。 我如何也可以添加其他鏈接?

確保將鏈接寫在for循環內。 使用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)

只需打開文件,然后寫你迭代:

 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) )

除非您希望所有鏈接都在一行中,否則在連接鏈接的末尾需要\\n 您的代碼還將寫入最后一個鏈接而不是第一個鏈接。

輸出:

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
.........................

以下代碼應允許您將多個行寫入文件。

 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) )

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

'w'打開進行寫入,首先截斷文件

每次以“ w”模式打開文件時,都會截斷它,這意味着丟棄所有現有內容。 您可能要保持打開狀態,直到完成寫入,然后再將其關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM