下载新浪博客文章,保存成文本文件(python)

本文介绍了一个使用Python编写的简单脚本,该脚本可以从韩寒的新浪博客批量下载文章,并将每篇文章保存为独立的文件。脚本会对文章进行格式化处理,去除HTML标签并替换特殊字符。

今天用Python写了一个下载韩寒新浪博客文章的下载器,恩,基本功能如下:

1、从新浪博客上批量下载文章,并按文章标题创建文件

2、对下载的文章进行格式化。

已知Bug:长篇文章格式会错乱

 1 #!/usr/bin/python
 2 #-*- coding:utf-8 -*-
 3 
 4 import urllib
 5 import os
 6 import re
 7 
 8 def article_format(usock,basedir):    
 9     title_flag=True
10     context_start_flag=True
11     context_end_flag=True
12     for line in usock:
13         if title_flag:
14             title=re.findall(r'(<title>.+?<)',line)
15             if title:
16                 title=title[0][7:-1]
17                 filename=basedir+title
18                 print filename
19                 try:
20                     fobj=open(filename,'w+')
21                     fobj.write(title+'\n')
22                     title_flag=False
23                 except IOError,e:
24                     print "Open %s error:%s"%(filename,e)
25             else:
26                 #print "Title has not found,drop it"
27                 pass
28         elif context_start_flag:
29             results1=re.findall(r'(<.+?正文开始.+?>)',line)
30             if results1:
31                 context_start_flag=False
32         elif context_end_flag:
33             results2=re.findall(r'(<.+?正文结束.+?)',line)
34             if results2:
35                 context_end_flag=False
36                 fobj.write('\nEND')
37                 fobj.close()
38                 break
39             else:    
40                 if 'div' in line or 'span' in line or  '<p>' in line:
41                     pass
42                 else:    
43                     line=re.sub('&#65292;',',',line)
44                     line=re.sub('&#65306;',':',line)
45                     line=re.sub('&#65281;','!',line)
46                     line=re.sub('&#65288;','(',line)
47                     line=re.sub('&#65289;',')',line)
48                     line=re.sub('&#8943;','...',line)
49                     line=re.sub('&#65311;','?',line)
50                     line=re.sub('&#65307;',';',line)
51                     line=re.sub(r'<wbr>','',line)
52                     line=re.sub(r'&nbsp;','',line)
53                     line=re.sub(r'<br\s+?/>','',line)
54                     fobj.write(line)
55         else:
56             pass
57 
58 if __name__=='__main__':
59     basedir='/home/tmyyss/article/'
60     if not os.path.exists(basedir):
61         os.makedirs(basedir)
62 
63     usock=urllib.urlopen("http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html")
64     context=usock.read()
65     #print context
66     raw_url_list=re.findall(r'(<a\s+title.+?href="http.+?html)',context)
67     for url in raw_url_list:
68         url=re.findall('(http.+?html)',url)[0]
69         article_usock=urllib.urlopen(url)
70         article_format(article_usock,basedir)
View Code

 

转载于:https://www.cnblogs.com/tmyyss/p/4169965.html

在Windows Form应用程序中,将TextBox控件中的文本内容保存文本文件可以按照以下步骤操作: 1. 创建一个TextBox控件(例如txtContent),用于输入或显示需要保存的内容。 ```csharp private TextBox txtContent; // 在Form_Load或其他适当的地方初始化 txtContent = new TextBox(); txtContent.Location = new Point(10, 10); this.Controls.Add(txtContent); ``` 2. 创建一个保存按钮(Button),并添加Click事件处理程序,在其中编写文件保存的代码: ```csharp private void btnSave_Click(object sender, EventArgs e) { string filePath = "C:\\textfile.txt"; // 文件路径,可以根据需求修改 string contentToSave = txtContent.Text; // 获取TextBox中的文本 try { using (StreamWriter writer = new StreamWriter(filePath)) { writer.WriteLine(contentToSave); // 写入文件 } MessageBox.Show("文本已保存到文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } ``` 3. 将保存按钮添加到窗体上,并设置其点击事件为刚刚创建的事件处理函数: ```csharp private Button btnSave; btnSave = new Button(); btnSave.Text = "保存"; btnSave.Location = new Point(10, 50); btnSave.Click += btnSave_Click; this.Controls.Add(btnSave); ``` 当你点击"保存"按钮时,会把TextBox里的文本保存到指定的文本文件中。如果需要定期或在其他条件触发时保存,你可以考虑更改`btnSave_Click`的触发方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值