自动生成html文件

C# code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Text; /// /// WriteFile 的摘要说明 /// public class WriteFile { public WriteFile() { } public static bool createHtml(string[] strnewsHtml,string[] stroldHtml,string strModeFilePath,string strPath) { bool flag = false; StreamReader sr = null; StreamWriter sw = null; string filepath = HttpContext.Current.Server.MapPath(strModeFilePath); Encoding code = Encoding.GetEncoding("gb2312"); string s = string.Empty; try { sr = new StreamReader(filepath,code); s = sr.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { sr.Close(); } try { for (int i = 0; i < strnewsHtml.Length; i++) { s = s.Replace(stroldHtml[i], strnewsHtml[i]); } sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code); sw.Write(s); flag = true; } catch (Exception ex) { flag = false; throw ex; } finally { sw.Flush(); sw.Close(); } return flag; } public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml) { bool Flage = false; StreamReader ReaderFile = null; StreamWriter WrirteFile = null; string FilePath = HttpContext.Current.Server.MapPath(strHtml); Encoding Code = Encoding.GetEncoding("gb2312"); string strFile = string.Empty; try { ReaderFile = new StreamReader(FilePath, Code); strFile = ReaderFile.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { ReaderFile.Close(); } try { int intLengTh = strNewsHtml.Length; for (int i = 0; i < intLengTh; i++) { int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length; int intEnd = strFile.IndexOf(strEndHtml[i]); string strOldHtml = strFile.Substring(intStart, intEnd - intStart); strFile = strFile.Replace(strOldHtml, strNewsHtml[i]); } WrirteFile = new StreamWriter(FilePath, false, Code); WrirteFile.Write(strFile); Flage = true; } catch (Exception ex) { throw ex; } finally { WrirteFile.Flush(); WrirteFile.Close(); } return Flage; } } 调用公共类: ---------------------------------------------------------------------------- protected void Button2_Click(object sender, EventArgs e) { string NewsTitle = this.TextBox1.Text; string NewsKindName = this.DropDownList1.SelectedItem.Text; string NewsBody = this.WebEditor1.Text; DateTime PubTime = DateTime.Now; string UserName = Session["UserName"].ToString(); Response.Write(NewsKindName); string[] strNewsHtml = new string[] { NewsTitle, NewsKindName, NewsBody, PubTime.ToString(), UserName }; string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" }; string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html"; string strFilePath = string.Format("NewsHtml/{0}", strFileName); try { if (WriteFile.createHtml(strNewsHtml, strOldHtml, "mode.htm", strFilePath)) { this.Label1.Text = "生成成功!"; } else { this.Label1.Text = "生成失败!"; } } catch { this.Label1.Text = "生成失败!"; } } protected void Button3_Click(object sender, EventArgs e) { string[] strNewsHtml=new string[]{"鑫少爷到此一游!"}; string[] strStartHtml=new string[]{" "}; string[] strEndHtml=new string[]{" "}; if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html")) { this.Label1.Text="生成首页成功!"; } else { this.Label1.Text="生成首页失败!"; } } 新建文件夹NewsHtml,生成html文件放在里面 ----------------------------------------------------------- 增加一个模板文件
@Title
发布人:@UserName     发布时间:@PubTime      新闻类别:@NewsKInd
@NewsBody
  
根据提供的引用内容,无法确定您是想要生成爬虫文件还是生成可执行文件。以下是两种可能的回答: 如果您是想要生成爬虫文件,可以使用Python中的requests和BeautifulSoup库来实现。具体步骤如下: 1.安装requests和BeautifulSoup库: ```shell pip install requests pip install beautifulsoup4 ``` 2.编写Python代码,使用requests库获取网页内容,使用BeautifulSoup库解析网页内容,提取需要的数据并保存到CSV文件中。以下是一个简单的示例代码: ```python import requests from bs4 import BeautifulSoup import csv import time # 获取当前时间 now = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime()) # 设置请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 发送请求 url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=%E7%83%AD%E6%90%9C%E6%A6%9C&fenlei=256&rsv_pq=8d7c7e7d0001c3d7&rsv_t=9b4d%2Bv%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B%2B&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=13&rsv_sug1=12&rsv_sug7=100&rsv_sug2=0&inputT=3473&rsv_sug4=3473' response = requests.get(url, headers=headers) # 解析网页内容 soup = BeautifulSoup(response.text, 'html.parser') hot_list = soup.find_all('a', {'class': 'list-title'}) # 保存数据到CSV文件 with open(now + '.csv', 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(['排名', '标题', '链接']) for i, item in enumerate(hot_list): writer.writerow([i + 1, item.text, item['href']]) ``` 如果您是想要生成可执行文件,可以使用pyinstaller库将Python代码打包成exe文件。具体步骤如下: 1.安装pyinstaller库: ```shell pip install pyinstaller ``` 2.在命令行中进入Python代码所在的目录,执行以下命令: ```shell pyinstaller -F your_script.py ``` 其中,your_script.py是您的Python代码文件名。 3.执行完上述命令后,会在dist目录下生成可执行文件。双击即可运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值