Find Text on web page

Discussion in 'Programming & Scripting' started by tOxiC x, Jan 14, 2009 with 12 replies and 191 views.

  1. tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Hey, I am trying to find text on a web page. The page is a .txt, so it would be easier to download this .txt each time the program runs (because it updates) and scan through this for the string that is inputed into textBox1. I don't want the text file to open, just to stay closed and a messagebox or label saying it is found. If someone could help me get started I would appreciate it, thanks.

     
  2. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Try something like this.
    Code:
    using System.IO;
    using System.Net;
    {
    string htmlText = string.Empty;
    StreamReader readerText = default(StreamReader);
    Stream WebStream = default(Stream);
    HttpWebResponse WebResponse = default(HttpWebResponse);
    HttpWebRequest request = WebRequest.Create("insert url to text file here");
    string SearchString = "Insert what you want to search for here";
    WebResponse = request.GetResponse();
    Webstream = Webresponse.GetResponseStream();
    readerText = new StreamReader(Webstream);
    htmlText = readerText.ReadToEnd();
    if (htmlText.Contains(SearchString)) {
    MessageBox.Show("Label Found");
    }
    else {
    MessageBox.Show("Label Not Found");
    }
    readerText.Close();
    WebStream.Close();
    WebResponse.Close();
    }
    
     
    • Like Like x 1
  3. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Thanks, I am going to try and fix it a little bit because it is full of errors, but thanks for the foundation.
     
  4. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Yeah sorry, I wrote it in VB and it worked fine, so I converted it to C# and just posted it. Don't really see the errors but whatever, as long as you got it working.
     
  5. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    I got it working, now just one more thing I need help with. Is there a way I can make the search exclude certain characters.

    For instance say I am searching for a recipe, and the title of the recipe is Boiled_Chicken, when I input Boiled Chicken, it won't work. I want to treat the _ as a space.
     
  6. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    yeah, converting isnt somthing you should do, it does to much stupid shizle to the code. Just have a friend translate it.
     
  7. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Well, I got the code working so the converter helped a lot. I just need what I said above. To make a space inputted into the textbox either _ or .

    The words in my text file can't have spaces so some have _ and some have .

    If it could just find the listed words maybe.

    ------

    Right now if I look for Mashed Potatoes, it won't come up because in the list it would be something like Mashed_Potatoes or Mashed.Potatoes.
     
  8. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    I could have written it in c#, but I don't have c# installed on this computer so that's why I converted it.

    Also on the text changed event for the textbox you could do something like

    if (TextBox1.Text.Contains(" ")) {
    TextBox1.Text = TextBox1.Text.Replace(" ", "_");
    }
     
    • Like Like x 1
  9. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    Lol, thanks for at least trying but that makes the textbox go crazy. Anyway, I would prefer for the textbox not to change, just do this unknowingly. Maybe Goldbl4d3 you know? I know your good at c#.
     
  10. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    Well scrap the whole textchanged thing then, but use that same method to take the spaces out of the SearchString, which I'm assuming you set equal to textbox1's text

    So use something like

    Code:
    If (SearchString.Contains(" ")) {
    SearchString = SearchString.Replace(" ", "_");
    }
     
  11. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    im not quite sure what your talking about.

    If you want to remove any spaces with somthing else just do:

    Code:
    textBox1.Text.Replace(" ", "_");
    If you want to remove the spaces then do:

    Code:
    textBox1.Text.Replace(" ", "");
    now if correctly done, you shouldnt see any change in your text box. But to see change just do the following as shown above:

    If you want to remove any spaces with somthing else just do:

    Code:
    textBox1.Text = textBox1.Text.Replace(" ", "_");
    If you want to remove the spaces then do:

    Code:
    textBox1.Text = textBox1.Text.Replace(" ", "");
    Or if your trying to add a space to the end try:

    Code:
    textBox1.Text += " ";
    if this doesnt help then explain what you need.
     
  12. OP
    tOxiC x

    tOxiC x Member

    Messages:
    656
    Ratings:
    116
    The problem is, it may be a "_" or a "."

    I'll explain this better.

    I want to make an app that connect to a web page (which is a .txt) and there are a list of recipies. I have a textbox and a button. I want to search for a recipie using that database on the webpage. However, in the txt file, there are no spaces. So when I search for "Mashed Potatoes", it won't find it because it is either "Mashed_Potates" or "Mashed.Potatoes" in the text.

    Instead of changing the "_" or "." to a space, would it be easier to just search per line in the text. For example, if it finds the word Mashed and Potatoes on the same LINE the result found will show up.
     
  13. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    i see.

    just make the string your searching for like this:

    this is all i could get atm, it has a few problems, like you can enter in the non full name and it will tell you it found it. But its close.

    This is just a set up example that should work

    Code:
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] txt = textBox1.Text;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sStr = textBox2.Text;[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] undrScor; [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] exists;[/SIZE]
    [SIZE=2]undrScor = txt.Contains(sStr.Replace([/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"_"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
    [SIZE=2]exists = txt.Contains(sStr.Replace([/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"_"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])) || [/SIZE]
    [SIZE=2]txt.Contains(sStr.Replace([/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (exists)[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (undrScor)[/SIZE]
    [SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show(sStr.Replace([/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"_"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008080][SIZE=2][COLOR=#008080]MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show(sStr.Replace([/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
    [SIZE=2]}[/SIZE]
    
     
    • Like Like x 1