using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using System.Threading;
namespace Factory
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//创建一个DataTable对象,用来保存商家的信息
DataTable dt= new DataTable();
private void MainForm_Load(object sender, EventArgs e)
{
//在DataTable中添加列
dt.Columns.Add("名称", System.Type.GetType("System.String"));
dt.Columns.Add("地址", System.Type.GetType("System.String"));
dt.Columns.Add("电话", System.Type.GetType("System.String"));
dt.Columns.Add("地址经纬度", System.Type.GetType("System.String"));
}
//根据Url地址得到网页的html源码
private 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("utf-8");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
//关闭读取流对象
streamReader.Close();
}
catch
{
// MessageBox.Show("请求出错。。。");
}
return strResult;
}
//点击按钮显示信息
private void btnShow_Click(object sender, EventArgs e)
{
string webUrl;
if (this.txtUrl.Text.Trim() == "")
{
// 要抓取的URL地址
webUrl = "http://www.dianping.com/search/category/2/10/g311";
}
else
{
webUrl = this.txtUrl.Text.Trim();
}
//得到指定Url的源码
string strContent = GetWebContent(webUrl);
//把换行符和空格符过滤掉
strContent = strContent.Replace("\r", "").Replace("\n", "");
rchCode.Text = strContent;
//获取商家的总数量
int count = Convert.ToInt32(GetSubString(ref strContent, "class=\"Color7\">(", ")"));
//保存商家信息的最大页数
int maxPage = 0;
if (count % 15 == 0)
{
maxPage = count / 15;
}
else
{
maxPage = (count / 15) + 1;
}
// 先清空
dt.Rows.Clear();
//for (int i = 1; i <= maxPage; i++)
//{
// GetDataByUrl(webUrl + "p" + i);
//}
GetDataByUrl(webUrl);
// 把获取的结果绑定到dataGridView上
this.dgvDetails.DataSource = dt;
}
private void GetDataByUrl(string url)
{
// 得到指定Url的源码
string strWebContent = GetWebContent(url);
for (int i = 0; i < 15; i++) // 每页显示15条好像是固定的
{
string name = GetSubString(ref strWebContent, ");\" target=\"_blank\" >", "</a>");
string addr = GetSubString(ref strWebContent, "\" class=\"Black-H\">", " ").Replace("</a>", "");
string tel = GetSubString(ref strWebContent, "", "</li>");
//获取经纬度
string latlng = GetlatlngByAddress(addr);
dt.Rows.Add(name, addr, tel,latlng);
}
}
// 截取字符串
private string GetSubString(ref string content, string beginStr, string endStr)
{
//开始位置
int begin = content.IndexOf(beginStr) + beginStr.Length;
//结束位置
int end = content.IndexOf(endStr, begin);
if (begin == -1 || end == -1 || begin > end)
return "索引出错。。。";
string result = content.Substring(begin, end - begin);
content = content.Remove(0, end + endStr.Length);
return result;
}
//把获取的商家信息保存到数据库中
private void btnSave_Click(object sender, EventArgs e)
{
BusDAL dal = new BusDAL();
if (dal.InsertBusinesser(dt) >= 1)
{
MessageBox.Show("插入成功!", "提示");
}
else
{
MessageBox.Show("插入失败!", "提示");
}
}
/// <summary>
/// 根据地址获取经度和纬度
/// </summary>
public string GetlatlngByAddress(string address)
{
string result = string.Empty;
try
{
//转换字符编码
address = HttpUtility.UrlEncode(address, Encoding.UTF8);
result = GetWebContent("http://maps.google.com/maps/geo?q=" + address + "&output=xml&sensor=true&key=dc27b1d958ee7725aa1b6899af7b50816258da9bf3ffa0f736db1bf3ca24877b7a25104e7f587e1c");
//解析所返回的带命名空间的xml并返回其中指定路径下的一个节点值
XmlDocument xml = new XmlDocument();
xml.LoadXml(result);
XmlNamespaceManager xmlns = new XmlNamespaceManager(xml.NameTable);
xmlns.AddNamespace("e", "http://earth.google.com/kml/2.0");
result = xml.SelectSingleNode("e:kml/e:Response/e:Placemark/e:Point/e:coordinates", xmlns).InnerText;
}
catch
{
result = null;
}
return result;
}
}
}