我正在尝试美味的汤,我想把它导出到一个文本文件中。
如何将result.txt
文件名更改为soup.find(class_="entry-title").get_text()
的文字?
提前致谢。
from bs4 import BeautifulSoup as bs
import urllib.request
#getting the page.
url = urllib.request.urlopen('http://www.haripuisi.com/arsip/3163').read()
soup = bs(url, 'lxml')
#extracting the content.
print (soup.find(class_="entry-title").get_text())
print (soup.find(class_="entry-content").get_text())
#save into text file.
save_file = open(r'C:\Users\Denis\Desktop\tempting texting\result.txt', 'w')
save_file.write(soup.find(class_="entry-content").get_text())
save_file.close()
答案 0 :(得分:0)
filename= soup.find(class_="entry-title").get_text()+".txt"
save_file = open(filename, 'w+')
save_file.close()
答案 1 :(得分:0)