<a onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');" href="javascript:void(0);" title="|升級 金屬礦 至 3 級" class="fastBuild tipsStandard">
<img width="22" height="14" src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif">
</a>
请问怎么取到a标签里面的“升級 金屬礦 至 3 級”中的“3”下面是我的通用方法: public String GetValueByRegex(String regex, String html)
{
String value = String.Empty; Regex reg = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase); value = reg.Match(html).Groups[1].Value; return value.Trim();
}必须在该方法中完成,为了节约代码,嘿嘿嘿该a标签在HTML里面,下面是代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="apple-touch-icon" href="http://gf1.geo.gfsrv.net/cdn68/20da7e6c416e6cd5f8544a73f588e5.png" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn01/9cba508bf487384321a92ee68144e4.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdn81/443dc7a9c57e71bdd94be2afb5d6be.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn4d/7f5cd54c0fdec17903f8ac4c9e1020.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn6a/525f931477f9f060322ae4f814794b.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdnd3/53de9cb06e2659c056b84c64ffe7ef.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn7f/4ec99f671704a6cd6fe6887f3685e2.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn3c/1078e92959f3090d99397615abaa67.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn49/cc7a154f3186f1e5776efeba902bf8.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn0d/cd69be5140eb480c6015f56700f75c.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdncd/7265b48a83720be5745b8c249b072b.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn4f/d2e0201b5fd0855780de262522fc8d.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnbd/d754e4c497e93186782db737e05fc8.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn0f/c6cc11701208612c4b2058be0d50d8.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdned/772b2d3c0e4346f2deb93edfcdbdf1.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnb7/a118fd8a870308fc26713a38a52cf0.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn3a/191b1e4950ccf07fe39a4b339877f7.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn60/5b59e9ae97d557f7ced89d29200cce.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn90/6ede3072b603ddfe67d7537b64be6d.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn07/a5f6217411b85b7587ac0b74065df2.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdnd1/42366c0bd29ce49bb28e55226151af.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnb2/a50d68dac8dfc5440fe9313223c25b.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdn5b/61bfbe660c957390107a232dd055db.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdnfb/e74ebc8d70274b9e33a8b9796bf1a5.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdncb/e9fc982e12ac3ada43f1f52da11325.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn6c/946a36b07fab1ccadb37905a455c42.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn99/f4996eca461e169934abd888ee3409.css"
media="screen" />
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn39/80f5848e31384cb807818228389547.css" media="screen" />
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnb6/e75a4bb4e9dc0ce3f7f7aac6a9c699.css" media="screen" />
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn42/2af7131df7c8210e7cd0bea1f3d9ff.css" media="screen" />
<![endif]-->
<title>Andromeda</title>
</head>
<body id="resources">
<div id="boxBG">
<div id="box">
<div id="links">
<div id="toolLinksWrapper">
</div>
<br class="clearfloat" />
</div>
<div id="inhalt">
<div id="buttonz">
<h2>
資源建築
</h2>
<ul id="building">
<li id="button1" class="on">
<div class="supply1">
<div class="buildingimg">
<a class="fastBuild tipsStandard" title="|升級 金屬礦 至 3 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a><a class="detail_button tipsStandard slideIn" title="|金屬礦" ref="1" id="details"
href="javascript:void(0);"><span class="ecke"><span class="level"><span class="textlabel">
金屬礦 </span>2 </span></span></a>
</div>
</div>
</li>
<li id="button2" class="on">
<div class="supply2">
<div class="buildingimg">
<a class="fastBuild tipsStandard" title="|升級 晶體礦 至 2 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=2&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a><a class="detail_button tipsStandard slideIn" title="|晶體礦" ref="2" id="details"
href="javascript:void(0);"><span class="ecke"><span class="level"><span class="textlabel">
晶體礦 </span>1 </span></span></a>
</div>
</div>
</li>
<li id="button3" class="on">
<div class="supply3">
<div class="buildingimg">
<a class="fastBuild tipsStandard" title="|升級 重氫合成器 至 2 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=3&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a><a class="detail_button tipsStandard slideIn" title="|重氫合成器" ref="3" id="details"
href="javascript:void(0);"><span class="ecke"><span class="level"><span class="textlabel">
重氫合成器 </span>1 </span></span></a>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="push">
</div>
</div>
</body>
</html>
解决方案 »
- ComponentOne C1 Studio2009 会出问题吗?
- socket开发,多终端,小数据量,应该采用什么方式好些.
- 怎样判断显卡有几个输出口?
- 我的VS2005 的 workflow fundation 设计器如何和其它的不同!已经装了最新的了。??急急急,请看详细
- 想请教一下 ,在浏览网站的时候,网站突然弹出窗口,管理员可以跟你对话,这个效果怎么做
- DataSet中表关系问题
- 急,我安装Duwamish7.0时,装到select a database那一步时,提示连接失败,未与sql sever相关联,我已经装上了sql sever2000服务器端了,
- C# Winform 右击按钮获取按钮属性 contextmenustrip
- 不通过类库项目,我能自由创建dll文件吗
- 高手哪里去?在C#中如何连上ORACLE数据库?在线等待!
- 在cmd中运行程序并传参数,错在哪儿
- 关于用代码删除datagrid中绑定数据表中的数据
<a onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');" href="javascript:void(0);" title="|升級 金屬礦 至 3 級" class="fastBuild tipsStandard">
<img width="22" height="14" src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif">
</a>
从HTML里面找到【title="|升級 金屬礦 至 3 級"】把里面的“3”取出来。
多谢啦。
public String GetValueByRegex(String regex, String html)
{
String value = String.Empty;
String num = String.Empty;
Regex reg = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase); value = reg.Match(html).Groups[1].Value;//title="|升級 金屬礦 至 3 級"
num= reg.Match(html).Groups[2].Value;//3
return value.Trim();
}
GetValueByRegex这个方法是通用得,如果该成Groups[2]的话,其他滴都得改吖。
(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title="\|.*?(\d+).*?"\s*class=""[^""]*"">
就可以public String GetValueByRegex(String regex, String html)
{
String value = String.Empty;
String num = String.Empty;
Regex reg = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase); value = reg.Match(html).Groups[1].Value;//3
return value.Trim();
}
GetValueByRegex(@"(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title="\|.*?(\d+).*?"\s*class=""[^""]*"">", html);
怎么这个字符串不正确呢?
费解,哎呀,我也太菜了囧。
GetValueByRegex(@"(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title=""\|.*?(\d+).*?""\s*class=""[^""]*"">", html);
不知道这边还有什么问题,没有结果。
在Demo里面也是。
我写的是直接在C#里面
在工具这么写
GetValueByRegex(@"(?is)<a\s*onclick="[^"]*"\s*href="[^"]*"\s*title="\|.*?(\d+).*?"\s*class="[^"]*">", html);
GetValueByRegex(@"(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title=""\|.*?(\d+).*?""\s*class=""[^""]*"">", html);
Resources[4] = GetValueByRegex(@"(?is)<a\b[^>]*?title=""\D+(\d)[^""]+""[^>]*?>", html);
Resources[4] = GetValueByRegex(@"(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title=""\|.*?(\d+).*?""\s*class=""[^""]*"">", html);
第一条得出的结果是101,成了后面onclick里面的url鸟
第二条,不知道为什么木有结果。囧。
<img width="22" height="14" src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif">
</a>
string input = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("gb2312"));
List<string> list = new List<string>();
string res = "";
foreach (Match m in Regex.Matches(input, @"(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title=""\|.*?(\d+).*?""\s*class=""[^""]*"">"))
{
Console.WriteLine(m.Groups[1].Value.Trim());
list.Add(m.Groups[1].Value.Trim());//3
}m.Groups[1].Value.Trim()得值就是3啊,测试了的
您用的是Regex.Matches,我这边用的是Match
就是这个通用方法,会不会是这里的问题? public String GetValueByRegex(String regex, String html)
{
String value = String.Empty; Regex reg = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase); value = reg.Match(html).Groups[1].Value; return value.Trim();
}
List<string> list = new List<string>();
Match m = Regex.Match(input, @"(?is)<a\s*onclick=""[^""]*""\s*href=""[^""]*""\s*title=""\|.*?(\d+).*?""\s*class=""[^""]*"">");
Console.WriteLine(m.Groups[1].Value.Trim());//3
list.Add(m.Groups[1].Value.Trim());
复制您的代码,然后新建的Demo
确实没结果,下面是截图。
txe里面不是那个a标签,应该放1楼最下面的HTML页面。
不好意思,刚刚试过了,您上面的代码放a标签是正确的。
<a onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');" href="javascript:void(0);" title="|升級 金屬礦 至 3 級" class="fastBuild tipsStandard"> <img width="22" height="14" src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif"> </a> 但是这个a标签来自于下面的HTML代码里面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="apple-touch-icon" href="http://gf1.geo.gfsrv.net/cdn68/20da7e6c416e6cd5f8544a73f588e5.png" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn01/9cba508bf487384321a92ee68144e4.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdn81/443dc7a9c57e71bdd94be2afb5d6be.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn4d/7f5cd54c0fdec17903f8ac4c9e1020.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn6a/525f931477f9f060322ae4f814794b.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdnd3/53de9cb06e2659c056b84c64ffe7ef.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn7f/4ec99f671704a6cd6fe6887f3685e2.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn3c/1078e92959f3090d99397615abaa67.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn49/cc7a154f3186f1e5776efeba902bf8.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn0d/cd69be5140eb480c6015f56700f75c.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdncd/7265b48a83720be5745b8c249b072b.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn4f/d2e0201b5fd0855780de262522fc8d.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnbd/d754e4c497e93186782db737e05fc8.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn0f/c6cc11701208612c4b2058be0d50d8.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdned/772b2d3c0e4346f2deb93edfcdbdf1.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnb7/a118fd8a870308fc26713a38a52cf0.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn3a/191b1e4950ccf07fe39a4b339877f7.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn60/5b59e9ae97d557f7ced89d29200cce.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn90/6ede3072b603ddfe67d7537b64be6d.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn07/a5f6217411b85b7587ac0b74065df2.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdnd1/42366c0bd29ce49bb28e55226151af.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnb2/a50d68dac8dfc5440fe9313223c25b.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdn5b/61bfbe660c957390107a232dd055db.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdnfb/e74ebc8d70274b9e33a8b9796bf1a5.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdncb/e9fc982e12ac3ada43f1f52da11325.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn6c/946a36b07fab1ccadb37905a455c42.css"
media="screen" />
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn99/f4996eca461e169934abd888ee3409.css"
media="screen" />
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="http://gf1.geo.gfsrv.net/cdn39/80f5848e31384cb807818228389547.css" media="screen" />
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="http://gf3.geo.gfsrv.net/cdnb6/e75a4bb4e9dc0ce3f7f7aac6a9c699.css" media="screen" />
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="http://gf2.geo.gfsrv.net/cdn42/2af7131df7c8210e7cd0bea1f3d9ff.css" media="screen" />
<![endif]-->
<title>Andromeda</title>
</head>
<body id="resources">
<div id="boxBG">
<div id="box">
<div id="links">
<div id="toolLinksWrapper">
</div>
<br class="clearfloat" />
</div>
<div id="inhalt">
<div id="buttonz">
<h2>
資源建築
</h2>
<ul id="building">
<li id="button1" class="on">
<div class="supply1">
<div class="buildingimg">
<a class="fastBuild tipsStandard" title="|升級 金屬礦 至 3 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a><a class="detail_button tipsStandard slideIn" title="|金屬礦" ref="1" id="details"
href="javascript:void(0);"><span class="ecke"><span class="level"><span class="textlabel">
金屬礦 </span>2 </span></span></a>
</div>
</div>
</li>
<li id="button2" class="on">
<div class="supply2">
<div class="buildingimg">
<a class="fastBuild tipsStandard" title="|升級 晶體礦 至 2 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=2&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a><a class="detail_button tipsStandard slideIn" title="|晶體礦" ref="2" id="details"
href="javascript:void(0);"><span class="ecke"><span class="level"><span class="textlabel">
晶體礦 </span>1 </span></span></a>
</div>
</div>
</li>
<li id="button3" class="on">
<div class="supply3">
<div class="buildingimg">
<a class="fastBuild tipsStandard" title="|升級 重氫合成器 至 2 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=3&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a><a class="detail_button tipsStandard slideIn" title="|重氫合成器" ref="3" id="details"
href="javascript:void(0);"><span class="ecke"><span class="level"><span class="textlabel">
重氫合成器 </span>1 </span></span></a>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="push">
</div>
</div>
</body>
</html>
上面给的<a onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');" href="javascript:void(0);" title="|升級 金屬礦 至 3 級" class="fastBuild tipsStandard"> <img width="22" height="14" src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif"> </a> 现在给的内容 <a class="fastBuild tipsStandard" title="|升級 金屬礦 至 3 級" href="javascript:void(0);"
onclick="sendBuildRequest('http://uni101.ogame.tw/game/index.php?page=resources&modus=1&type=1&menge=1&token=2a6dba6e36336e12dd094d09cbda3e3f');">
<img src="http://gf2.geo.gfsrv.net/cdndf/3e567d6f16d040326c7a0ea29a4f41.gif" width="22"
height="14" />
</a>改成这样 string input = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("gb2312"));
List<string> list = new List<string>();
MatchCollection m = Regex.Matches(input, @"(?is)<a\s*class=""[^""]*""\s*title=""\|.*?(\d+).*?""\s*href=""[^""]*""\s*onclick=""[^""]*"">.*?</a>");
foreach (Match mx in m)
{
Console.WriteLine(mx.Groups[1].Value.Trim());
list.Add(mx.Groups[1].Value.Trim());
}
/*
3
1
2
*/
您这个确实能取出来,不过不应该是3、1、2
应该是3、2、2
升級 金屬礦 至 3 級
升級 晶體礦 至 2 級
升級 重氫合成器 至 2 級而且在我的通用方法里面得出的结果任然是“101”Demo里面的正则通用方法是: public String GetValueByRegex(String regex, String html)
{
String value = String.Empty; Regex reg = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase); value = reg.Match(html).Groups[1].Value; return value.Trim();
}
string input = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("gb2312"));
Dictionary<string, string> dic = new Dictionary<string, string>();
MatchCollection m = Regex.Matches(input, @"(?is)<a\s*class=""fastBuild tipsStandard""\s*title=""\|(.*?(\d+).*?)""\s*href=""[^""]*""\s*onclick=""[^""]*"">.*?</a>");
foreach (Match mx in m)
{
dic.Add(mx.Groups[1].Value, mx.Groups[2].Value.Trim());
Console.WriteLine(mx.Groups[1].Value + "\t" + mx.Groups[2].Value.Trim());
}
/*
升級 金屬礦 至 3 級 3升級 晶體礦 至 2 級 2升級 重氫合成器 至 2 級 2
*/
既然这样我就把我测试用的帐号拿出来:
http://uni101.ogame.tw/game/reg/login2.php?v=2&login=abc123a&pass=ads35fasd
然后再用这个地址:
http://uni101.ogame.tw/game/index.php?page=resources
最终就是要得到这个页面中的3、2、2这问题也纠结了怎么久了实在过意不去。
{
String value = String.Empty; Regex reg = new Regex(regex, RegexOptions.Singleline | RegexOptions.IgnoreCase); value = reg.Match(html).Groups[1].Value; return value.Trim();
}
升級 晶體礦 至 2 級 2
升級 重氫合成器 至 2 級 2
其实最开始就是想通过这3句话将数字找到。title="|升級 金屬礦 至 3 級"
title="|升級 晶體礦 至 2 級"
title="|升級 重氫合成器 至 2 級"能实现吗?
因为还有6个页面,每次都是重新请求的。