I am working on collecting urls from the web site in C# using WatiN framework. In my program it is fetching only one url. I don't know what is the problem. Any help will be appreciated.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
namespace magicbricks
{
class scroll
{
[STAThread]
static void Main(string[] args)
{
Browser browserInstance;
browserInstance = new IE(#"http://www.99acres.com/property-in-chennai- ffid?search_type=QS&search_location=CP32&lstAcn=CP_R&lstAcnId=32&src=CLUSTER&isvoicesearch=N&keyword_suggest=chennai%20%28all%29%3B&fullSelectedSuggestions=chennai%20%28all%29&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiY2hlbm5haSAoYWxsKSIsIkNJVFlfMzIsIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=chennai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_32%2C%20PREFERENCE_S%2C%20RESCOM_R");
foreach (var links in browserInstance.Links.Filter(Find.ByClass("b")))
{
Console.WriteLine(links.Url);
String filePath = "C:/Users/User/Desktop/New folder";
String fileName = "newop4.csv";
using (StreamWriter sr = new StreamWriter(Path.Combine(filePath, fileName), true))
{
sr.WriteLine(links.Url);
}
Console.ReadLine();
}
}
}
}
the above code prints only one url in the console.
Remove the Console.ReadLine(); As you are in a ForEach loop. If you still want the Console.ReadLine(); move it out the foreach
The Console.ReadLine(); waits for a user input, after you enter any value you should see the next URL.
Related
I have many files in folder. and whenever there is any update in any file I Receive an event for that in my windows service application.
And I am looking for something by which I can validate the file with specific pattern. If it matches then only that file should be processed or else it should be ignored.
Something like this
if(File.Matches("genprice*.xml"))
{
DoSomething();
}
genprice20212604.xml
genprice20212704.xml
price20212604.xml
genprice20212704.txt
From above only #1 and #2 should be processed others should be ignored.
Your can try with regular expressions:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ConsoleAppRegex
{
class Program
{
static void Main(string[] args)
{
string[] fileNames = new string[] { "genprice20212604.xml",
"genprice20212704.xml",
"price20212604.xml",
"genprice20212704.txt"};
Regex re = new Regex(#"genprice[^\.]*.xml");
foreach (string fileName in fileNames)
{
if (re.Match(fileName).Success)
{
Console.WriteLine(fileName);
}
}
Console.ReadLine();
}
}
}
I suggest to use Regex:
using System.Text.RegularExpressions;
using System.IO;
var reg = new Regex(#"genprice\d{8}$");
var fileNamesFromFolder = Direcotory.GetFiles(" #FolderĀ“s path ", "*.xml")
.Where(path => reg.IsMatch(Path.GetFileNameWithoutExtension(path)))
.Select(Folder=>
Path.GetFileNameWithoutExtension(Folder));
foreach (var file in fileNamesFromFolder )
{
//Do something...
}
I'm trying to take html code from a page on the internet, save it as a text file, then read the text file find a part of the code, save it as a var and out put it to the console in c#.
this is the code i'm trying but it doesn't work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class StringSearch
{
static void Main()
{
string HTML = System.IO.File.ReadAllText(#"C:\Users\gamer\Desktop\HTML\code test.txt");
string sPattern = "code";
foreach (string s in HTML)
{
System.Console.Write("{0,24}", s);
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", sPattern);
}
else
{
System.Console.WriteLine();
}
}
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
P.S if you know a way to capture a pages HTML code/part of a pages HTML code and out put it that would be even better
Thanks
I'm trying to get a simple text parser class to work in VS2015. I received the class code and built a basic Console Application, added the class Cawk and tried to compile/run it.
The main error that I get is
Argument 1: cannot convert from 'string' to 'System.IO.StreamReader'
It's clear that I can't figure out how to pass a filename through Main to Cawk. How do I give it an argument of a filename?
Any help or pointers would be appreciated.
My Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
string input = #"c:\temp\test.txt";
Cawk.Execute(input);
}
}
}
Snippet of My Cawk.cs:
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication3
{
public static class Cawk
{
public static IEnumerable<Dictionary<string, object>> Execute(StreamReader input)
{
Dictionary<string, object> row = new Dictionary<string, object>();
string line;
//string[] lines = File.ReadAllLines(path);
//read all rows
while ((line = input.ReadLine()) != null)
{
Execute accepts a StreamReader not a string.
Cawk.Execute(new StreamReader(#"c:\temp\test.txt"))
However, you should close the stream after you are done with it.
using (var sr = new StreamReader(#"c:\temp\test.txt"))
{
Cawk.Execute(sr);
}
something like:
var sr = new System.IO.StreamReader(#"c:\temp\test.txt");
Cawk.Execute(sr);
Simply use the File class from the System.IO namespace.
Cawk.Execute(File.OpenText(#"c:\temp\test.txt"));
Like this:
string input = #"c:\temp\test.txt";
Cawk.Execute(new System.IO.StreamReader(input));
You can put using System.IO; to the top like the rest of the usings, then you don't have to write it out later.
I have a small problem. I just recently started using Twilio's API to generate a record of messages that was sent to my assigned SID and Auth Token. However my question is how can I generate a text file, based off of what the console writes from the source its addressed to?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Twilio;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/user/account
string AccountSid = "X";
string AuthToken = "X";
var twilio = new TwilioRestClient(AccountSid, AuthToken);
// Build the parameters
var options = new MessageListRequest();
options.From = "2015-07-01";
options.To = "2015-07-13";
var messages = twilio.ListMessages(options);
foreach (var message in messages.Messages)
{
Console.WriteLine(message.Body);
Console.Read();
}
}
}
}
Writing to a text file is pretty much boilerplate. The methods are shown here:
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
Wrote code as the start to a web crawler that scrapes links from webpage.
Following the instructions from this page:
http://www.dotnetperls.com/scraping-html
I seem to get an error that LinkFinder cannot be found?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace WebCrawler
{
class Program
{
static void Main(string[] args)
{
WebClient url = new WebClient();
String initialLink = url.DownloadString("http://www.FAKEADDRESS.org.uk/");
for (LinkItem i in LinkFinder.find(initialLink))
{
System.Diagnostics.Debug.WriteLine(initialLink);
}
}
}
}
LinkFinder is a class that is included in the code at that URL you provided. Make sure you also copy that class into your project in some way (a file by itself, in another file, whatever).