摘要:运用php对网页内容获取便利专业的特点,采用php站点访问目标站点,获取数据后,把变量输出为json格式。再通过C#访问php站点获得json格式文件,进行解析,入库。
示例:
将要截取的table部分通过php解析,并生成json格式字符串;(代码1)
C#程序获取网页字符串,并解析json内容,使用SQLHelper,可以到我的资源那里下载使用,入库。
代码:
php
(1)
$res = file_get_contents($url);
$ss = utftogbk($res);
preg_match_all("/(\<table.*\<\/table\>)/sU", $ss, $matches);
preg_match_all("/\<td\>(.*)\<\/td\>/", $matches[0][0], $matches);
foreach ($matches[1] as $k=>$v) {
//$v = str_replace(" ", $replace, $subject);
$key = intval($k/4);
if(stristr($v, "<h2")){
$v = str_replace("<h2 class=\"officers\">", "", $v);
$v = preg_replace("/<a href.*link\"\>/", "", $v);
$v = str_replace("</a></h2>", "", $v);
}
$new_arr [$key][] = $v;
}
echo json_encode($new_arr);die();
C#
(2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DataHelper;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Net.Json;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string sql = "SELECT [stockcode] FROM [HKSO].[dbo].[stock] where stockcode >980 and stockcode<1000";
SqlDataReader dr = SQLHelper.ExecuteReader(sql);
while (dr.Read())
{
string code = dr[0].ToString();
if (code.Length != 4)
{
switch (code.Length)
{
case 1:
code = "000" + code;
break;
case 2:
code = "00" + code;
break;
case 3:
code = "0" + code;
break;
default:
break;
}
}
//catch the page
#region get content
WebRequest request = WebRequest.Create("http://192.168.0.104/my/test.php?url=http://www.reuters.com/finance/stocks/companyOfficers?symbol=" + code + ".HK");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
#endregion
//json
responseFromServer = responseFromServer.Replace(" ", " ");
JsonTextParser parser = new JsonTextParser();
JsonObject obj = parser.Parse(responseFromServer);
List<JsonObject> objlist = (List<JsonObject>)obj.GetValue();
for (int i = 0; i < objlist.Count; i++)
{
List<JsonObject> objlist1 = (List<JsonObject>)objlist[i].GetValue();
string name = (string)objlist1[0].GetValue();
string age = (string)objlist1[1].GetValue();
string since = (string)objlist1[2].GetValue();
string position = (string)objlist1[3].GetValue();
SQLHelper.ExecuteSql("insert into dbo.Officer (companycode,officer,age,since,currentposition) values ('"+code+"','"+name+"','"+age+"','"+since+"','"+position+"')");
}
}
}
}
}