0

I am making a proxy scraper program and i need to find the proxies in an array

Here is an example of what i want to get out of this line:

document.write('77.237.138.51')

I want to remove document.write('" and "') so it only shows the proxy

Here is my current code:

client.DownloadFile("http://www.gatherproxy.com/sockslist", "source.txt"); 
string [] lines = File.ReadAllLines("source.txt");
string start = "document.write('";
string end = "')";

Now how would I make it so where i can delete start and end and return the middle element (the proxy)

In reply to Domysee

using (WebClient client = new WebClient())
            client.DownloadFile("http://www.gatherproxy.com/sockslist", "source.txt");
            string[] lines = File.ReadAllLines("source.txt");
        for (int i = 0; i < 1000; i++)
        {
            string[] ipAddresses = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray();
            i++;
            string[] port = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray();
            Console.WriteLine(ipAddresses + ":" + port);
        }
            Console.ReadLine();
7
  • So source.txt contains lines in the format "document.write('11.111.111.11')" and you need "11.111.111.11" from it? Commented Mar 5, 2016 at 8:26
  • Yes, that is correct. Commented Mar 5, 2016 at 8:28
  • Does your source document contain other lines that have IP addresses? Commented Mar 5, 2016 at 8:35
  • @Enigmativity Yes, it does and I want to retrieve them all Commented Mar 5, 2016 at 8:37
  • @Zezima - So you also want to retrieve IP address that aren't surrounded by document.write(' & ')? Commented Mar 5, 2016 at 8:37

2 Answers 2

1

You can utilize Regex for this purpose.

string[] ipAddresses = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray();

The regex will extract the bit that corresponds to the ip address.

ipAddresses is an array of strings. If you concatenate it with another string (as you're doing in Console.WriteLine(ipAddresses + ":" + port);, its ToString method will be called, which is "System.String[]".

To output the ip addresses you have to iterate over the array.

string[] lines = File.ReadAllLines("source.txt");
string[] ipAddresses = lines.Select(l => Regex.Match(l, @"(\d+\.){3}\d+").Value).ToArray();
for(int i = 0; i < ipAddresses.Length; i++){
    Console.WriteLine(ipAddresses[i]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I just wouldn't reuse lines. Variables are cheap.
System.String[] is returning instead of the IP address. Am I missing something?
@Zezima How are you using the ipAddresses variable?
@Domysee prntscr.com/abfbof here's a screenshot of my code, also forgot to mention I am new to C#
@Zezima I cannot view the page, why dont you update your post with the code?
0

You can use LINQ:

string[] lines = File.ReadAllLines("source.txt");

string[] ipAddresses = lines.Select(line => String.Join("", line.SkipWhile(c => c != '\'')
                                                                .Skip(1)
                                                                .TakeWhile(c => c != '\'')))
                            .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.