关于从一个网站页面中抓取图片的问题。 那不成了采编系统了!图片所在的a.htm网页绝对地址取出来,不可能 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只要知道图片的url就可以了啊,获得网页源代码,http的get方法,分析字符串,把image标记的src属性取出来就可以了 啊 那么怎么样从a.htm页面中取出图片的url呢? 谢谢 给你参考一下:(通过获取网页内容进行分析) private void btnGet_Click(object sender, System.EventArgs e) { WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.html"); HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse(); string HTML =""; Stream s=wresp.GetResponseStream(); StreamReader objReader = new StreamReader(s); string sLine = ""; int i = 0; while (sLine!=null) { i++; sLine = objReader.ReadLine(); if (sLine!=null) HTML += sLine; } String temp= ""; int start,stop; start = HTML.IndexOf("<!-- CURCON-->",0,HTML.Length); stop = HTML.IndexOf("<!-- END CURCON-->",0,HTML.Length); temp = HTML.Substring(start, stop - start); start = temp.IndexOf("<b>"); stop = temp.IndexOf("</b>"); string degree = temp.Substring(start+3,stop - start -3); start = temp.IndexOf("<img"); stop = temp.IndexOf("</td>",start); string img = temp.Substring(start,stop - start); lblWeather.Text = degree + "<br>" + img; } 只要知道图片的url就可以了啊,获得网页源代码,http的get方法,分析字符串,把image标记的src属性取出来就可以了 啊说这话的人没有大脑 3d_viz,我能获得网页源代码,但是怎么样把这个html源码中所有图片显示出来啊?谢谢。 我就是想实现这样一个功能:(1)通过WebRequest从一个网站读一个页面后,仅只显示出这个页面全部图片怎么实现?(2)然后任意点一个图片的超链接,这个图片的url就自动的存在一个字符串变量中,最后通过程序保存在数据库字段中如果实现? 有几种方式实现你的要求:如果在ASP中,需要使用微软的XMLHTTP下载网页的内容(如你所说的a.htm),然后使用VBScript正则表达式提取图片的url路径,再用XMLHTTP下载图片,然后保存到你的服务器(可以同时改名为你需要的文件名)。如果在Asp.net中,相对简单很多,用WebClient下载网页数据成字符串变量,使用正则表达式提取图片的url路径,再下载图片,保存到数据库。以上方法我已全部搞定,包括下载网页保存到数据库(可以同时转为繁体),下载图片到本地服务器同时改名并生成缩略图,但基于版权问题不能给你,所以给你一个思路。如有需要可以私下联系,但要保护版权。[email protected] anbeel(Coding人生)说的行吗?表示怀疑,看看有没有其他的办法。 johnsuna(缘来是e) ,谢谢你的指点。^_* 可惜你有版权。有知道的朋友来指点一下。多谢! 有了新进展。我现在可以取出页面中的所有连接了。^_*不过离目标还有点远。<%@Page Language="c#"%><%@Import Namespace="System" %><%@Import Namespace="System.Net" %><%@Import Namespace="System.Text" %><%@Import Namespace="System.IO" %><script runat="server">public void Page_Load(object sender, System.EventArgs e){ string link=null; string str; int curloc; string uristr="http://www.hs169.com/type.asp?typeID=1"; try { do { Response.Write("Linking to "+uristr); HttpWebRequest req=(HttpWebRequest)WebRequest.Create("http://www.hs169.com/type.asp?typeID=1"); uristr=null; HttpWebResponse resp=(HttpWebResponse)req.GetResponse(); Stream istrm=resp.GetResponseStream(); StreamReader rdr=new StreamReader(istrm,System.Text.Encoding.GetEncoding("GB2312")); str=rdr.ReadToEnd(); curloc=0; do { link=FindLink(str,ref curloc); if(link!=null) { Response.Write(link+"<br>"); } else { Response.Write("No link found."); break; } } while(link.Length>0); resp.Close(); } while(uristr!=null); } catch(WebException exc) { Response.Write("Network Error:"+exc.Message+"\nStatus code:"+exc.Status); } catch(ProtocolViolationException exc) { Response.Write("Protocol Error:"+exc.Message); } catch(UriFormatException exc) { Response.Write("URI Format Error:"+exc.Message); } catch(NotSupportedException exc) { Response.Write("Unknown Protocol:"+exc.Message); } catch(IOException exc) { Response.Write("I/O Error:"+exc.Message); } Response.Write("Terminating MiniCrawler.");} static string FindLink(string htmlstr,ref int startloc) { int i; int start,end; string uri=null; string lowcasestr=htmlstr.ToLower(); i=lowcasestr.IndexOf("<a ",startloc); if(i!=-1) { start=i; end=htmlstr.IndexOf("</a>",start); uri=htmlstr.Substring(start,end-start); startloc=end; } return uri; }</script> 新手:关于login控件编辑的问题 求助:一对多关系,gridview实现 CS文件控制li问题 后台输出服务器控件LINkbutton 请教:gridview中updatecommand如何使用? 求一小算法~~~~大家一起来帮忙帮忙。~~~ datalist,datagrid,repeater显示数据哪一个更快 ?? 使用Response.Redirect()方法时,出现“线程序被终止”的错误 JQuery获取多个ID,无法对多个ID进行操作,急! 怎么生成唯一编号 insert语句出错! 生成XML数据的问题
{
WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.html");
HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
string HTML ="";
Stream s=wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
String temp= "";
int start,stop;
start = HTML.IndexOf("<!-- CURCON-->",0,HTML.Length);
stop = HTML.IndexOf("<!-- END CURCON-->",0,HTML.Length);
temp = HTML.Substring(start, stop - start);
start = temp.IndexOf("<b>");
stop = temp.IndexOf("</b>");
string degree = temp.Substring(start+3,stop - start -3);
start = temp.IndexOf("<img");
stop = temp.IndexOf("</td>",start);
string img = temp.Substring(start,stop - start);
lblWeather.Text = degree + "<br>" + img;
}
但是怎么样把这个html源码中所有图片显示出来啊?谢谢。
(1)通过WebRequest从一个网站读一个页面后,仅只显示出这个页面全部图片怎么实现?
(2)然后任意点一个图片的超链接,这个图片的url就自动的存在一个字符串变量中,最后通过程序保存在数据库字段中如果实现?
如果在ASP中,需要使用微软的XMLHTTP下载网页的内容(如你所说的a.htm),然后使用VBScript正则表达式提取图片的url路径,再用XMLHTTP下载图片,然后保存到你的服务器(可以同时改名为你需要的文件名)。
如果在Asp.net中,相对简单很多,用WebClient下载网页数据成字符串变量,使用正则表达式提取图片的url路径,再下载图片,保存到数据库。
以上方法我已全部搞定,包括下载网页保存到数据库(可以同时转为繁体),下载图片到本地服务器同时改名并生成缩略图,但基于版权问题不能给你,所以给你一个思路。如有需要可以私下联系,但要保护版权。[email protected]
不过离目标还有点远。<%@Page Language="c#"%>
<%@Import Namespace="System" %>
<%@Import Namespace="System.Net" %>
<%@Import Namespace="System.Text" %>
<%@Import Namespace="System.IO" %>
<script runat="server">
public void Page_Load(object sender, System.EventArgs e)
{
string link=null;
string str; int curloc; string uristr="http://www.hs169.com/type.asp?typeID=1";
try
{
do
{
Response.Write("Linking to "+uristr);
HttpWebRequest req=(HttpWebRequest)WebRequest.Create("http://www.hs169.com/type.asp?typeID=1");
uristr=null; HttpWebResponse resp=(HttpWebResponse)req.GetResponse(); Stream istrm=resp.GetResponseStream(); StreamReader rdr=new StreamReader(istrm,System.Text.Encoding.GetEncoding("GB2312")); str=rdr.ReadToEnd(); curloc=0; do
{
link=FindLink(str,ref curloc); if(link!=null)
{
Response.Write(link+"<br>");
}
else
{
Response.Write("No link found.");
break; }
}
while(link.Length>0);
resp.Close();
}
while(uristr!=null);
}
catch(WebException exc)
{
Response.Write("Network Error:"+exc.Message+"\nStatus code:"+exc.Status);
}
catch(ProtocolViolationException exc)
{
Response.Write("Protocol Error:"+exc.Message); }
catch(UriFormatException exc)
{
Response.Write("URI Format Error:"+exc.Message);
}
catch(NotSupportedException exc)
{
Response.Write("Unknown Protocol:"+exc.Message);
}
catch(IOException exc)
{
Response.Write("I/O Error:"+exc.Message); } Response.Write("Terminating MiniCrawler.");
}
static string FindLink(string htmlstr,ref int startloc)
{
int i;
int start,end;
string uri=null;
string lowcasestr=htmlstr.ToLower();
i=lowcasestr.IndexOf("<a ",startloc);
if(i!=-1)
{
start=i;
end=htmlstr.IndexOf("</a>",start);
uri=htmlstr.Substring(start,end-start);
startloc=end;
}
return uri;
}
</script>