抓取百度搜索后打开指定网页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Diagnostics;

public partial class index : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
string urlPage = getContent("http://www.baidu.com/s?rn=50&bs=%C9%EE%DB%DA%CD%E2%BB%E3%BF%AA%BB%A7%C9%EE%DB%DA%CD%E2%BB%E3%BB%C6%BD%F0%BF%AA%BB%A7%D0%C2%CA%D6%B3%B4%CD%E2%BB%E3%B2%D9%C5%CC%CA%D618718691836&f=8&wd=%C9%EE%DB%DA%CD%E2%BB%E3%BF%AA%BB%A7%C9%EE%DB%DA%CD%E2%BB%E3%BB%C6%BD%F0%BF%AA%BB%A7%D0%C2%CA%D6%B3%B4%CD%E2%BB%E3%B2%D9%C5%CC%CA%D6");
        
int star1 = 0;
        
int star2 = 0;
        
int end1 = 0;
        
int end2=0;
        
string url = "";
        
string context="";
        star1 
= urlPage.IndexOf("<a onmousedown=\"return");
        
while (star1 > 0)     
        {
            star2 
= urlPage.IndexOf("href=\"",star1)+6//6为搜索字符串的长度
            end1 = urlPage.IndexOf("\" target=\"_blank\"",star2); 
            url 
= urlPage.Substring(star2, end1 - star2);
            end2
=urlPage .IndexOf ("/h3",end1);
            context 
=urlPage .Substring (end1,end2-end1);
            
if (context.IndexOf("<em>深圳外汇开户</em>,深圳炒外汇炒<em>黄金</em>,<em>新手炒外汇操盘手</em>-福田福田周边担">= 0)
            {
                
//打开网页
                
//getContent(url);
                ////System.Diagnostics.Process.Start("IEXPLORE.EXE", url);

                Process   pr   
=   new   Process(); 
                
//参数位置是用于执行程序的,在命令提示符里运行都可以有结果 
                ProcessStartInfo strtInfo = new ProcessStartInfo("IEXPLORE.EXE", url); 
                
//strtInfo.WindowStyle   =   ProcessWindowStyle.Hidden; 
                pr.StartInfo   =   strtInfo;
                pr.Start();
                System.Threading.Thread.Sleep(
10000);

                
//Process[] p = Process.GetProcessesByName("iexploer.exe");
                
//foreach (Process process in p)
                
//{
                
//    process.Kill();
                
//
                pr.Kill();
                pr.Dispose();

                
//跳出重写搜索
                break;
            }
            
else
            {
                
//截取,后面的不要,重新搜索
                urlPage = urlPage.Substring(end2);
                star1 
= urlPage.IndexOf("<a onmousedown=\"return");
            }
        }
    }
    
//抓取网页
    private static string getContent(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();
            streamReader.Close();
        }
        
catch
        {
            
throw;
        }
        
return strResult;
    }  
}
原文地址:https://www.cnblogs.com/ringwang/p/2031618.html