当前位置: 移动技术网 > IT编程>开发语言>.net > C#获取页面上的Html

C#获取页面上的Html

2019年04月19日  | 移动技术网IT编程  | 我要评论

济南在线,望奎天气预报,丹漠洞


//根据url地址得到网页的html源码
public static string getwebcontent(string url)
{
  string strresult = "";
  try
  {
    httpwebrequest request = (httpwebrequest)webrequest.create(url);
    //声明一个httpwebrequest请求
    request.timeout = 30000;
    //设置连接超时时间
    request.headers.set("pragma", "no-cache");
    httpwebresponse response = (httpwebresponse)request.getresponse();
    stream streamreceive = response.getresponsestream();
    encoding encoding = encoding.getencoding("gb2312");
    streamreader streamreader = new streamreader(streamreceive, encoding);
    strresult = streamreader.readtoend();
  }
  catch
  {
    console.writeline("出错");
  }
    return strresult;
  }

 

public string search(string argaddress)
{
  //要抓取的url地址
  stringbuilder sb = new stringbuilder();
  //sb.append("http://ditu.google.cn/maps");
  //sb.append("?");
  //sb.append("hl=zh-cn&newwindow=1&safe=strict");
  //sb.append("&q=");
  //sb.append(argaddress);
  //sb.append("&bav=on.2,or.&bvm=bv.44158598,d.dgi&biw=1440&bih=775&um=1&ie=utf-8&sa=n&tab=wl");
  sb.append("http://query.customs.gov.cn/mnftq/mquery.aspx");

  //得到指定url的源码
  string strwebcontent = getwebcontent(sb.tostring());

  ////生成htmldocument
  //webbrowser webb = new webbrowser();
  //webb.navigate("about:blank");
  //htmldocument htmldoc = webb.document.opennew(true);
  //htmldoc.write(strwebcontent);
  //htmlelementcollection htmltr = htmldoc.getelementsbytagname("tr");
  //foreach (htmlelement tr in htmltr)
  //{
    // string address = string.empty;


    // try
    // {
      // string resultspanel = tr.document.getelementbyid("resultspanel").document.getelementbyid("panel_a_2").innertext;

      // string[] contentlines = resultspanel.split(new string[] { "\r\n" }, stringsplitoptions.none);//不忽略空行

      // address = contentlines[1];
    // }
    // catch { };


    // //插入datatable
    // if (address != string.empty)
    // {
      // return address;
    // }
    // else
    //  {
      // continue;
    // }
  //}

  return strwebcontent;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网