获取html内标题,通过html敏捷包获取标题标签

我正在尝试使用htmlagility pack来获得结果的链接和结果

我有这个代码

using HtmlAgilityPack;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Net;

using System.ServiceModel.Syndication;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Xml;

namespace Search

{

public partial class Form1 : Form

{

// load snippet

HtmlAgilityPack.HtmlDocument htmlSnippet = new HtmlAgilityPack.HtmlDocument();

public Form1()

{

InitializeComponent();

}

private void btn1_Click(object sender, EventArgs e)

{

listBox1.Items.Clear();

StringBuilder sb = new StringBuilder();

byte[] ResultsBuffer = new byte[8192];

string SearchResults = "http://google.com/search?q=" + txtKeyWords.Text.Trim();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SearchResults);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream resStream = response.GetResponseStream();

string tempString = null;

int count = 0;

do

{

count = resStream.Read(ResultsBuffer, 0, ResultsBuffer.Length);

if (count != 0)

{

tempString = Encoding.ASCII.GetString(ResultsBuffer, 0, count);

sb.Append(tempString);

}

}

while (count > 0);

string sbb = sb.ToString();

HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();

html.OptionOutputAsXml = true;

html.LoadHtml(sbb);

HtmlNode doc = html.DocumentNode;

foreach (HtmlNode link in doc.SelectNodes("//a[@href]"))

{

//HtmlAttribute att = link.Attributes["href"];

string hrefValue = link.GetAttributeValue("href", string.Empty);

if (!hrefValue.ToString().ToUpper().Contains("GOOGLE") && hrefValue.ToString().Contains("/url?q=") && hrefValue.ToString().ToUpper().Contains("HTTP://"))

{

int index = hrefValue.IndexOf("&");

if (index > 0)

{

hrefValue = hrefValue.Substring(0, index);

listBox1.Items.Add(hrefValue.Replace("/url?q=", ""));

}

}

}

}

}

}

此代码返回查询的结果链接我想获得每个链接的标题标签我如何获得每个链接的标题?

任何人都可以帮忙吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值