Div集合只打印一个值
本文关键字:一个 集合 打印 Div | 更新日期: 2023-09-27 18:12:22
你好,我正在尝试打印div集合。在打印div集合时,我只获得第一个值。这是我的程序。
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
using FileHelpers;
using RealEstate.Core;
using ConsoleApplication3;
using System.Collections.Specialized;
namespace ConsoleApplication4
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
Browser bi;
bi = new IE(@"https://www.proptiger.com/gurgaon/sector-37c/ild-grand-501804");
string BHK_Collection = string.Empty;
string Total_Units_details = string.Empty;
DivCollection collection = bi.Divs.Filter(Find.ByClass("projImgContBoxInfo ng-scope"));
foreach (var Span in collection)
{
if (Span.OuterText.Contains("Basic information"))
{
BHK_Collection = Span.OuterText.Replace(" Request a site visit", "").Replace("Basic information", "").Replace("This configuration is available from builder", "").Replace("2D 3D Video Tour", "").Replace("'r'n", "'n").Replace("Bedroom(s)", "Bedroom(s);").Replace("Bath(s)", "Bath(s);").Replace("Balconie(s)", "Balconie(s);").Replace("Price On Request", "Price On Request;").Replace("Servant Room", "Servant Room;").Replace("sq ft", "sq ft;").Replace("Cr*", "Cr;").Replace("Request a floor plan", "").Replace("(", "").Replace(")", "").Replace("− Builder Price", "").Replace("Lacs*", "Lacs;").TrimStart();
Total_Units_details = string.Join(" ", BHK_Collection.Split(''r', ''n')).Replace(" ", "").Replace(" ", " ").Replace(System.Environment.NewLine, " ").TrimStart();
}
}
Console.WriteLine(Total_Units_details);
}
}
}
实际输出是
2BHK+2T - 1,280 sq ft ;66.6 Lacs;5,200 / sq ft;2 Bedroom(s);2 Bath(s);4 Balconie(s);EMI - 54,995
2BHK+2T - 1,310 sq ft ; 68.1 Lacs;5,200 / sq ft;2 Bedroom(s);2 Bath(s);3 Balconie(s);EMI - 56,233
3BHK+3T – 1,790 sq ft; 88.6 Lacs;4,950 / sq ft; 3 Bedrooms;3 Baths; 3 Balconies;EMI - 74,400
3BHK+3T – 1,820 sq ft; 90.1 Lacs;4,950 / sq ft; 3 Bedrooms;3 Baths;;EMI - 74,400
3BHK+4T Study – 2,289 sq ft; 1.66 Cr;7,250 / sq ft; 3 Bedrooms;4 Baths; 5 Balconies;EMI - 1,40,377
但是我的程序只打印一个值。
不是追加Total_Units_details
,而是在循环的每次迭代中分配一个新值。
你可以使用:
foreach (var Span in collection)
{
if (Span.OuterText.Contains("Basic information"))
{
BHK_Collection = Span.OuterText.Replace(" Request a site visit", "").Replace("Basic information", "").Replace("This configuration is available from builder", "").Replace("2D 3D Video Tour", "").Replace("'r'n", "'n").Replace("Bedroom(s)", "Bedroom(s);").Replace("Bath(s)", "Bath(s);").Replace("Balconie(s)", "Balconie(s);").Replace("Price On Request", "Price On Request;").Replace("Servant Room", "Servant Room;").Replace("sq ft", "sq ft;").Replace("Cr*", "Cr;").Replace("Request a floor plan", "").Replace("(", "").Replace(")", "").Replace("− Builder Price", "").Replace("Lacs*", "Lacs;").TrimStart();
//CHANGE HERE :
Total_Units_details = Total_Units_details + Environment.NewLine + string.Join(" ", BHK_Collection.Split(''r', ''n')).Replace(" ", "").Replace(" ", " ").Replace(System.Environment.NewLine, " ").TrimStart();
}
}
(字符串连接):"第一行" +环境。
也尝试查找类StringBuilder
,它比使用上面的方法更好。你可以在循环的每次迭代中将新的String附加到StringBuilder中,然后在此之后附加Environment.NewLine
class Program
{
[STAThread]
public static void Main(string[] args)
{
Browser bi;
bi = new IE(@"https://www.proptiger.com/gurgaon/sector-37c/ild-grand-501804");
string BHK_Collection = string.Empty;
string Total_Units_details = string.Empty;
DivCollection collection = bi.Divs.Filter(Find.ByClass("projImgContBoxInfo ng-scope"));
foreach (var Span in collection)
{
if (Span.OuterText.Contains("Basic information"))
{
BHK_Collection = Span.OuterText.Replace(" Request a site visit", "").Replace("Basic information", "").Replace("This configuration is available from builder", "").Replace("2D 3D Video Tour", "").Replace("'r'n", "'n").Replace("Bedroom(s)", "Bedroom(s);").Replace("Bath(s)", "Bath(s);").Replace("Balconie(s)", "Balconie(s);").Replace("Price On Request", "Price On Request;").Replace("Servant Room", "Servant Room;").Replace("sq ft", "sq ft;").Replace("Cr*", "Cr;").Replace("Request a floor plan", "").Replace("(", "").Replace(")", "").Replace("− Builder Price", "").Replace("Lacs*", "Lacs;").TrimStart();
Total_Units_details = string.Join(" ", BHK_Collection.Split(''r', ''n')).Replace(" ", "").Replace(" ", " ").Replace(System.Environment.NewLine, " ").TrimStart();
}
}
Console.WriteLine(Total_Units_details);
}
}
如果要将每行输出到控制台,则需要将Console.WriteLine
移动到foreach
循环
foreach (var Span in collection)
{
if (Span.OuterText.Contains("Basic information"))
{
BHK_Collection = Span.OuterText.Replace(" Request a site visit", "").Replace("Basic information", "").Replace("This configuration is available from builder", "").Replace("2D 3D Video Tour", "").Replace("'r'n", "'n").Replace("Bedroom(s)", "Bedroom(s);").Replace("Bath(s)", "Bath(s);").Replace("Balconie(s)", "Balconie(s);").Replace("Price On Request", "Price On Request;").Replace("Servant Room", "Servant Room;").Replace("sq ft", "sq ft;").Replace("Cr*", "Cr;").Replace("Request a floor plan", "").Replace("(", "").Replace(")", "").Replace("− Builder Price", "").Replace("Lacs*", "Lacs;").TrimStart();
Total_Units_details = string.Join(" ", BHK_Collection.Split(''r', ''n')).Replace(" ", "").Replace(" ", " ").Replace(System.Environment.NewLine, " ").TrimStart();
Console.WriteLine(Total_Units_details);
}
}