美丽的汤 - 导出到文本文件

时间:2017-09-26 08:09:58

标签: python beautifulsoup

我正在尝试美味的汤,我想把它导出到一个文本文件中。

如何将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()

2 个答案:

答案 0 :(得分:0)

filename= soup.find(class_="entry-title").get_text()+".txt"
save_file = open(filename, 'w+')
save_file.close()

答案 1 :(得分:0)

试试这段代码,我使用python2.7,所以你需要更改下面的内容:

       private void btn_startnetwork_Click(object sender, EventArgs e)
    {
        connection = new Connection();
        connection.Run();
        connection.ChangeCommand("wlan start hostednetwork");
        connection.Run();
    }

我收到文件: enter image description here