Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从网页c获取值#_C#_Html - Fatal编程技术网

C# 从网页c获取值#

C# 从网页c获取值#,c#,html,C#,Html,我有一个包含网页代码的字符串 这是一个例子: 在这个字符串中,我想得到650和250(这些是变量,它们会改变值) 我怎样才能做到 例如: 名称 价值 x4b08 254 x4b07 253 x4b06 252 x4b05 251 您可以使用正则表达式匹配value=“([0-9]*)” 或者,您可以使用string.IndexOf查找字符串“value”,然后使用以下几个字符。如果您确信标记永远不会更改(并且您有一个类似于示例行的简单片段),则正则表达式可以获取这些值,例如: Regex r

我有一个包含网页代码的字符串

这是一个例子:


在这个字符串中,我想得到
650
250
(这些是变量,它们会改变值)

我怎样才能做到

例如:

名称 价值 x4b08 254 x4b07 253 x4b06 252 x4b05 251
您可以使用正则表达式匹配
value=“([0-9]*)”


或者,您可以使用
string.IndexOf
查找字符串“value”,然后使用以下几个字符。

如果您确信标记永远不会更改(并且您有一个类似于示例行的简单片段),则正则表达式可以获取这些值,例如:

Regex re = new Regex("name=\"(.*?)\" value=\"(.*?)\""); 
Match match = re.Match(yourString); 
if(match.Success && match.Groups.Count == 3){ 
    String name = match.Groups[1]; 
    String value = match.Groups[2];
}
或者,您可以解析页面内容并查询结果文档中的元素,然后提取值。(C#HTML解析器:)

这应该适合您(假设s包含要解析的字符串):


你的例子有多具体?您还想提取不同长度的字母字符串吗?要提取的字符串是否总是属性

虽然regex/substring方法适用于指定的示例,但我认为它们的伸缩性非常差


我会使用解析器(参见ndtreviv的答案)或者可能使用XML解析器(如果HTML是有效的XHTML)解析HTML。这样,您就可以获得更好的控制,而不必因为摆弄一大堆正则表达式而大惊小怪。

如果您有多个字符串形式的控件,您可以创建和XmlDocument并对其进行迭代。

刚刚解决了这个问题

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream st = resp.GetResponseStream();
StreamReader sr = new StreamReader(st);
string buffer = sr.ReadToEnd();

ArrayList uniqueMatches = new ArrayList();
Match[] retArray = null;
Regex RE = new Regex("name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.Multiline);
MatchCollection theMatches = RE.Matches(buffer);

for (int counter = 0; counter < theMatches.Count; counter++)
{
//string[] tempSplit = theMatches[counter].Value.Split('"');

Regex reName = new Regex("name=\"(.*?)\"");
Match matchName = reName.Match(theMatches[counter].Value);

Regex reValue = new Regex("value=\"(.*?)\"");
Match matchValue = reValue.Match(theMatches[counter].Value);

string[] dados = new string[2];
dados[0] = matchName.Groups[1].ToString();
dados[1] = matchValue.Groups[1].ToString();
uniqueMatches.Add(dados);
}
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse resp=(HttpWebResponse)req.GetResponse();
Stream st=resp.GetResponseStream();
StreamReader sr=新StreamReader(st);
字符串缓冲区=sr.ReadToEnd();
ArrayList uniqueMatches=新的ArrayList();
Match[]retArray=null;
Regex RE=new Regex(“name=\”(.*?\“value=\”(.*?\”),RegexOptions.Multiline);
MatchCollection theMatches=RE.Matches(缓冲区);
对于(int counter=0;counter

Tks all for the help

如果我想得到这个?name=“x4B07”value=“650”,名称和相应的值?Regex re=new Regex(“name=\”(.*?”)“value=\”(.*?);Match Match=re.Match(yourString);if(Match.Success&&Match.Groups.Count==3){name=Match.Groups[1];value=Match.Groups[2];}-我将继续,并将其添加到我的答案中,以使其更具可读性!然后,您必须生成两行代码来提取名称,并在其中使用s.IndexOf(“name=”)+6,而不是上面编写的代码。这只返回第一个元素,如何返回其他元素?例如,创建一个数组?ou list?。名称返回为x8000”type=“hiddenhi,页面有几个名称和值,我想要的是类似于我将插入的示例的内容。这只返回第一个元素,如何返回其他元素?例如,创建一个数组?ou list?。名称返回为x8000”type=“hiddenTo要从文档的某个部分获取多个名称/值对,请继续执行if((match=match.NextMatch())!=null){name=match.Groups[1];value=match.Groups[2];}在文档中检查它:和PS:显然,要在列表/数组或您选择的集合中创建,只需在迭代时将值添加到该集合中。迭代=编程101。
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream st = resp.GetResponseStream();
StreamReader sr = new StreamReader(st);
string buffer = sr.ReadToEnd();

ArrayList uniqueMatches = new ArrayList();
Match[] retArray = null;
Regex RE = new Regex("name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.Multiline);
MatchCollection theMatches = RE.Matches(buffer);

for (int counter = 0; counter < theMatches.Count; counter++)
{
//string[] tempSplit = theMatches[counter].Value.Split('"');

Regex reName = new Regex("name=\"(.*?)\"");
Match matchName = reName.Match(theMatches[counter].Value);

Regex reValue = new Regex("value=\"(.*?)\"");
Match matchValue = reValue.Match(theMatches[counter].Value);

string[] dados = new string[2];
dados[0] = matchName.Groups[1].ToString();
dados[1] = matchValue.Groups[1].ToString();
uniqueMatches.Add(dados);
}