PDA

View Full Version : چگونگی خواندن متن عنوان یک پست از وبلاگ (بلاگفا)



nurani
پنج شنبه 09 اردیبهشت 1395, 14:29 عصر
می خوام عنوان رو در تکست باکس نمایش دهم
اینم یه نمونه
http://sazmanbarnameh.blogfa.com/post/321

syntiberium
جمعه 10 اردیبهشت 1395, 08:39 صبح
using System.Net;

string contents = new System.Net.WebClient().DownloadString("http://sazmanbarnameh.blogfa.com/post/321");
string[] s1 = contents.Split(new char[] { '\r' });
for (int i = 0; i < s1.Count(); i++)
{
int i1 = s1[i].IndexOf("<b><a href=\"/post");
if (i1 != -1)
{
string[] s2 = s1[i].Split(new char[] { '>' });
string[] s3 = s2[2].Split(new char[] { '<' });
textBox1.Text = s3[0];
}
}

من اینو برات نوشتم ولی فونت فارسیش رو درست نشون نمیده .

ramtinak
جمعه 10 اردیبهشت 1395, 17:02 عصر
using System.Net;

string contents = new System.Net.WebClient().DownloadString("http://sazmanbarnameh.blogfa.com/post/321");
string[] s1 = contents.Split(new char[] { '\r' });
for (int i = 0; i < s1.Count(); i++)
{
int i1 = s1[i].IndexOf("<b><a href=\"/post");
if (i1 != -1)
{
string[] s2 = s1[i].Split(new char[] { '>' });
string[] s3 = s2[2].Split(new char[] { '<' });
textBox1.Text = s3[0];
}
}

من اینو برات نوشتم ولی فونت فارسیش رو درست نشون نمیده .

سلام، چون بلاگفا از فشرده ساز gzip استفاده می کنه.
WebClient به صورت خودکار نمی تونه متون رو از حالت فشرده در بیاره، در این حالت بهتره از HttpWebRequest استفاده بشه:

var request = (HttpWebRequest)WebRequest.Create("http://sazmanbarnameh.blogfa.com/post/321");
request.Headers.Add(HttpRequestHeader.AcceptEncodi ng, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string contents = new System.IO.StreamReader(response.GetResponseStream( )).ReadToEnd();
string[] s1 = contents.Split(new char[] { '\r' });
for (int i = 0; i < s1.Count(); i++)
{
int i1 = s1[i].IndexOf("<b><a href=\"/post");
if (i1 != -1)
{
string[] s2 = s1[i].Split(new char[] { '>' });
string[] s3 = s2[2].Split(new char[] { '<' });
textBox1.Text = s3[0];
}
}



جدا از این چرا اینقدر کار رو برای یک Index کردن ساده سخت کردی؟

var request = (HttpWebRequest)WebRequest.Create("http://sazmanbarnameh.blogfa.com/post/321");
request.Headers.Add(HttpRequestHeader.AcceptEncodi ng, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string contents = new System.IO.StreamReader(response.GetResponseStream( )).ReadToEnd();


string startIndex = "<b><a href=\"/post";
string postName = contents.Substring(contents.IndexOf(startIndex) + startIndex.Length);
postName = postName.Substring(postName.IndexOf(">") + 1);
postName = postName.Substring(0, postName.IndexOf("</"));
textBox1.Text = postName;


موفق باشید.

nurani
شنبه 11 اردیبهشت 1395, 12:05 عصر
چرا بعضی از عنوان ها رو نمی خونه مانند اینها
http://yeganehdownloada.blogfa.com/post/53
http://heydarabadi.blogfa.com/post/261

ramtinak
سه شنبه 14 اردیبهشت 1395, 07:01 صبح
دلیلش اینه که ما داریم از سورس کد HTML استفاده می کنیم، تم هر سایت یا وبلاگی با همدیگه فرق می کنه اما معمولا تگی به نام Title دارن که می شه عناوین رو خوند:

var request = (HttpWebRequest)WebRequest.Create("http://yeganehdownloada.blogfa.com/post/53");
request.Headers.Add(HttpRequestHeader.AcceptEncodi ng, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string contents = new System.IO.StreamReader(response.GetResponseStream( )).ReadToEnd();




string startIndex = "<title>";
string postName = contents.Substring(contents.IndexOf(startIndex) + startIndex.Length);
postName = postName.Substring(0, postName.IndexOf("</title>"));
textBox1.Text = postName;