从网站解析数据 [英] Parse Data from web site
问题描述
大家好,我正在尝试解析网站上的数据,我收到一个字符串,如
                             + 9%
来自这些我只需要(9)如何将此数字检索到变量?
我使用修剪,删除和其他功能,他们正在为字符串工作,但在这里他们不工作,现在为什么。
这是我正在使用的代码:
WebClient wc =新的WebClient();
string htmlString = wc.DownloadString(http://sentra.com.gr/index.php?option = com_content& view = article& id = 3291:rekor-times-oloklirothike-dimoprasia-kopenhagen-fur-dekembrios-2012& catid = 75:auction& Itemid = 141);
string price =;
匹配mprice = Regex.Match(htmlString,@
Chinchilla(。*?)
Hi guys, I'm trying to parse data from a website and I'm receiving a string like
+9%
from these I just need (9) how can I retrieve this number to a variable?
I use Trim, Remove and other functios that they are working for strings but here they are not working and now why.
Here is the code that I'm using:
WebClient wc = new WebClient();
string htmlString = wc.DownloadString("http://sentra.com.gr/index.php?option=com_content&view=article&id=3291:rekor-times-oloklirothike-dimoprasia-kopenhagen-fur-dekembrios-2012&catid=75:auction&Itemid=141");
string price = "";
Match mprice = Regex.Match(htmlString, @"
Chinchilla(.*?)
,RegexOptions。单线);
if(mprice.Success)
{
price = mCountry.Groups [1] .Value;
TextBox1.Text = price;
airplaneCountry.Remove(0,81);
Console.Write线(airplaneCountry);
// res.Text =价格;
}
price = Regex.Replace(价格,(。* ?)>,)。Trim();
TextBox2.Text =价格;
thnx提前
Jason
", RegexOptions.Singleline);
if (mprice.Success)
{
price = mCountry.Groups[1].Value;
TextBox1.Text = price;
airplaneCountry.Remove(0,81);
Console.WriteLine(airplaneCountry);
// res.Text = price;
}
price = Regex.Replace(price, "(.*?)>", "").Trim();
TextBox2.Text = price;
thnx in advance
Jason
推荐答案
根据您的字符串类型而定处理,以及你正在抓取:你可能使用不同策略的信息类型。
如果你只想要数字,写一个RegEx除去数字之外的一切。类似于:
Depending on what type of strings you are processing, and what types of information you are "scraping:" you might use different strategies.
If you want only numbers, write a RegEx that removes everything but numbers. Something like:
YourString = Regex.Replace(YourString, "[^0-9.]", "");
就你的例子而言:+ 9% :只需要删除百分号以获得有效整数:
In the case of your example : "+9%" : only the percent sign needs to be removed to get a valid integer:
private int i;
private bool IsNumber;
private string n = "+9%";
private void SomeMethod(string testString)
{
n = n.TrimEnd('%');
IsNumber = Int32.TryParse(n, out i);
if (IsNumber)
{
// code to handle the valid number
// in the variable i
}
}
这篇关于从网站解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!