消除or替代字串 抓取特定html tag 本帖最后由 jason36900 于 2014-09-06 00:33:15 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 (1)用htmldecode先转码,再用trim(2)贴出html代码片段,好提供给你一个正则表达式来过滤 第一和第二都可以用正则表达式来处理处理转义字符比较简单,HTML 就那么几种转义字符,可以使用正则表达式一次性进行替换。获取 img 的也可以使用正则表达式来处理,例如,底下是一个简单的示例打印出页面中的图片。 static void Main(string[] args) { HttpWebRequest request = HttpWebRequest.CreateHttp("http://www.baidu.com"); using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream())) { string line = default(string); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[<|\\s]+img src=\"([^\"]+)\""); while ((line = reader.ReadLine()) != null) { System.Console.WriteLine(line); if (regex.IsMatch(line)) { foreach (System.Text.RegularExpressions.Match match in regex.Matches(line)) { System.Console.WriteLine(match.Groups[1].Value); } } } } System.Console.ReadKey(); } <div id="InnerTopicLeft"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td><img src="images/InnerTopicIcon001.jpg" alt="標題" border="0" /></td> <td id="InnerTopic">LINE跑跑薑餅人愛心接力跑!</td> </tr> </table> </div> <div id="InnerTopicRight">大概是這個片段要抓到<td><img 裡面的照片但問題在於 我一次想要抓取的文章有9筆但不是每筆都有照片且並不一定都是放在div 跟td裡面 (有的是<p>裡)所以想請問能不能鎖定抓到img裡面的圖就好而不是固定抓到td呢?請大哥賜教 C# 的异常处理 C# 用IF判断数据库是否有相同数据后,在执行其他SQL语句就超时。 webClient定时post问题 [100F]求教如何获取本程序集的guid值,或获取其他程序集的guid值呢?? C#怎样指定行来读取汉字的文本啊,高手帮忙 如何使BindingSource不自动调用BeginEdit sql控制台程序问题 大家支持一下,收集jacaScript脚本——谢谢! DataGrid的选中行问题,请高手指点! 异步socket如何通过防火墙(防火墙对端口有限制) IIS网站URL如何重定向? 我来虚心请教,ODBC操作EXCEL的一些疑问
(2)贴出html代码片段,好提供给你一个正则表达式来过滤
处理转义字符比较简单,HTML 就那么几种转义字符,可以使用正则表达式一次性进行替换。获取 img 的也可以使用正则表达式来处理,例如,底下是一个简单的示例打印出页面中的图片。
static void Main(string[] args)
{
HttpWebRequest request = HttpWebRequest.CreateHttp("http://www.baidu.com");
using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
{
string line = default(string);
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[<|\\s]+img src=\"([^\"]+)\""); while ((line = reader.ReadLine()) != null)
{
System.Console.WriteLine(line); if (regex.IsMatch(line))
{
foreach (System.Text.RegularExpressions.Match match in regex.Matches(line))
{
System.Console.WriteLine(match.Groups[1].Value);
}
}
}
} System.Console.ReadKey();
}
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/InnerTopicIcon001.jpg" alt="標題" border="0" /></td>
<td id="InnerTopic">LINE跑跑薑餅人愛心接力跑!</td>
</tr>
</table>
</div>
<div id="InnerTopicRight">大概是這個片段
要抓到<td><img 裡面的照片
但問題在於 我一次想要抓取的文章有9筆
但不是每筆都有照片
且並不一定都是放在div 跟td裡面 (有的是<p>裡)
所以想請問能不能鎖定抓到img裡面的圖就好
而不是固定抓到td呢?
請大哥賜教