How do I search for a quoted string?
I am looking for a line
<!--m--><li class="g w0"><h3 class=r><a href="
in the HTML source of this link:
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=Santarus+Inc
This is how I look for it:
string html_string = "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=" + biocompany;
html = new WebClient().DownloadString(html_string);
d=html.IndexOf(@"<!--m--><li class=""g w0""><h3 class=r><a href=""",1);
For some reason, he detects his appearance at position 45 (in other words, d = 45), but this is not true.
Here are the first few hundred characters of the HTML string:
<!doctype html><head><title>Santarus Inc‎ - Google Search</title><script>window.google={kEI:\"b6jES5nPD4rysQOokrGDDQ\",kEXPI:\"23729,24229,24249,24260,24414,24457\",kCSI:{e:\"23729,24229,24249,24260,24414,24457\",ei:\"b6jES5nPD4rysQOokrGDDQ\",expi:\"23729,24229,24249,24260,24414,24457\"},ml:function(){},kHL:\"en\",time:function(){return(new Date).getTime()},log:function(b,d,c){var a=new Image,e=google,g=e.lc,f=e.li;a.onerror=(a.onload=(a.onabort=function(){delete g[f]}));g[f]=a;c=c||\"/gen_204?atyp=i&ct=\"+b+\"&cad=\"+d+\"&zx=\"+google.time();a.src=c;e.li=f+1},lc:[],li:0,Toolbelt:{}};\nwindow.google.sn=\"web\";window.google.timers={load:{t:{start:(new Date).getTime()}}};try{}catch(u){}window.google.jsrt_kill=1;\n</script><style>body{background:#fff;color:#000;margin:3px 8px}#gbar,#guser{font-size:13px;padding-top:1px !important}#gbar{float:left;height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh
a source to share
Post more code. I am assuming your html variable does not store what you think. Are you reading html line by line, by accident? And are you adding or replacing the previous content? Anyway, put a breakpoint just before or after the call to .IndexOf and check the html content.
Edit . I ran through your code sample and didn't find your line.
string biocompany = "Santarus Incβ";
string html_string = "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=" + biocompany;
using (WebClient client = new WebClient())
{
string html = client.DownloadString(html_string);
int d = html.IndexOf(@"<!--m--><li class=""g w0""><h3 class=r>");
Console.WriteLine(d);
}
I'm not sure what you are doing based on the code you posted.
a source to share
You are not looking for the line you are talking about.
If you are looking for this:
<!--m--><li class="g w0"><h3 class=r>
Use this:
@"<!--m--><li class=""g w0""><h3 class=r>"
Not:
@"<!--m--><li class=""g w0""><h3 class=r><a href="""
Update: (following comments)
I ran a google search on the url you used. I have not found a case with a tag <li>
with the specified attribute class
. Are you looking for the correct string?
a source to share
Anthony gave the correct solution.
But it is also true that browsers show that the html being loaded is different than the one loaded in code.
This is why the returned index becomes -1 and not 45 as stated in "every_answer_gets_a_point" :)
This is another question in itself - why is this happening.
As for searching, Anthony has already given an answer.
a source to share