Friday, May 1, 2015

利用htmlunit和webdriver读取网页数据

先说.net的,需要进入 using mshtml;

           System.Net.WebClient webClient = new System.Net.WebClient();
              IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://course.xxx.edu/");
            String pageSource = driver.PageSource;
这种比较笨重,需要启动firefox读取


对java的,需要使用htmlunit包
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_31);
webClient.getOptions().setJavaScriptEnabled(true);
//webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setThrowExceptionOnScriptError(false);
   webClient.getOptions().setCssEnabled(false);

   HtmlPage page =
webClient.getPage("http://course.xxx.edu");
 
webClient.waitForBackgroundJavaScript(5000);


String text=page.asText();
/*String patternString = "Available Seats:";
Pattern pattern = Pattern.compile(patternString);

        Matcher matcher = pattern.matcher(text);*/
        int index=text.lastIndexOf("Available Seats");
        String seats=text.substring(index+17, index+19).trim();

int numOfSeats=Integer.parseInt(seats);
System.out.println(numOfSeats);


如何利用c#发送gmail
public static void SendMail(String s)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("sender@gmail.com");
            mail.To.Add("receiver@gmail.com");
           // mail.To.Add("m2@gmail.com");
            mail.Subject = "good news: "+s;
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";
            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";
            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";
            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "mypassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

        }

No comments:

Post a Comment