C# 获取网页标题title、keywords、description 的代码(winform版本,.net版本照样可以用)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Library.SEO; using System.Net;//其下有WebRequest和WebResponse using System.IO;//用到StreamReader using System.Text.RegularExpressions;//正则表达式,比截取字符串要好用的多 namespace SeoTools { /// <summary> /// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ /// Copyright (C) http://www.taobaonzpd.com/ | http://www.anmoqi10.com/ | http://www.hainandh.com/ | http://www.tb10go.com/ | http://www.tbshc.com/ /// 文件名:E:\创业\站长必备工具\SEOTools\SEOTools\frmSiteAdd.cs /// 作 者:李文辉 /// 日 期:2011.12.19 /// 描 述:新增网站信息 /// 版 本:Seo工具之外链工具----李文辉个人版本 /// 修改历史纪录 /// 版 本 修改时间 修改人 修改内容 /// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ /// </summary> public partial class frmSiteAdd : Form { public frmSiteAdd() { InitializeComponent(); } private void btnSEO_Click(object sender, EventArgs e) { string httpUrl = txtsite_url.Text.Trim(); if (string.IsNullOrEmpty(txtsite_url.Text)) { MessageBox.Show("网址不能为空!"); txtsite_url.Focus(); return; } string charSet = "GB2312";//utf-8 WebRequest oRequest = WebRequest.Create(httpUrl); WebResponse oResponse = oRequest.GetResponse(); StreamReader oReader = new StreamReader(oResponse.GetResponseStream(), Encoding.GetEncoding(charSet)); string html = oReader.ReadToEnd(); Match m1 = Regex.Match(html, "<title>(.*)</title>"); Match m2 = Regex.Match(html, @"(?s)(.+?)\s*<meta\s*name=\""keywords\""\s*content=\""([^\""]+).+?<meta\s*name=\""description\""\s*content=\""([^\""]+)"); if (m1.Groups.Count == 2) { txttitle.Text = m1.Groups[1].Value;//获取title } txtkey_words.Text = m2.Groups[2].Value;//获取keywords txtdescription.Text = m2.Groups[3].Value;//获取description } }}