用到HtmlAgilityPack,Newtonsoft.Json
1. [代码][C#]代码
//设置listview的相关属性
listView1.GridLines
=
true
;
listView1.View
= View.Details;
listView1.LabelEdit
=
true
;
listView1.Sorting
= SortOrder.None;
//排序,根据第一列
listView1.Columns.Add(
"歌手"
,100,
HorizontalAlignment.Left);
//参数:列名、宽度、对齐方式
listView1.Columns.Add(
"收听量"
,
300, HorizontalAlignment.Left);
2. [代码][C#]代码
WebClient
wc =
new
WebClient();
wc.Encoding
= Encoding.UTF8;
string
html = wc.DownloadString(
new
Uri(
"http://y.qq.com/y/static/singer/index/all_hot_1.html"
));
HtmlAgilityPack.HtmlDocument
doc =
new
HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
string
nodexpath =
""
;
//获取图片排序,10个人
for
(
int
i = 1; i < 11; i++)
{
nodexpath
= String.Format(
"/html/body/div/div/div[2]/div/div[2]/ol/li[{0}]/h5/span[1]/a"
,
i);
HtmlNode
base1 = doc.DocumentNode.SelectSingleNode(nodexpath);
//获取歌手的id
string
getstr = base1.Attributes[
"href"
].Value.Substring(21,14);
WebClient
wd =
new
WebClient();
string
jsurl = String.Format(
"http://s.plcloud.music.qq.com/fcgi-bin/fcg_order_singer_getnum.fcg?singermid={0}&rnd=1420900900825&g_tk=974283127&loginUin=529324801&hostUin=0&format=jsonp&inCharset=GB2312&outCharset=gb2312¬ice=0&platform=yqq&jsonpCallback=MusicJsonCallback&needNewCode=0"
,
getstr);
string
jsresult = wd.DownloadString(
new
Uri(jsurl));
jsresult
= jsresult.Replace(
"MusicJsonCallback("
,
""
);
int
n = 0;
n
= jsresult.LastIndexOf(
")"
);
jsresult
= jsresult.Substring(0, n);
JObject
jo = (JObject)JsonConvert.DeserializeObject(jsresult);
ListViewItem
item =
new
ListViewItem();
item.Text
= base1.InnerText;
item.SubItems.Add(jo[
"num"
].ToString());
listView1.Items.AddRange(
new
ListViewItem[] { item });
}
//获取文字排序,40个人
for
(
int
i = 1; i < 41; i++)
{
nodexpath
= String.Format(
"/html/body/div/div/div[2]/div/div[3]/ol/li[{0}]/span[1]/a"
,
i);
HtmlNode
base1 = doc.DocumentNode.SelectSingleNode(nodexpath);
string
getstr = base1.Attributes[
"href"
].Value.Substring(21,
14);
WebClient
wd =
new
WebClient();
string
jsurl = String.Format(
"http://s.plcloud.music.qq.com/fcgi-bin/fcg_order_singer_getnum.fcg?singermid={0}&rnd=1420900900825&g_tk=974283127&loginUin=529324801&hostUin=0&format=jsonp&inCharset=GB2312&outCharset=gb2312¬ice=0&platform=yqq&jsonpCallback=MusicJsonCallback&needNewCode=0"
,
getstr);
string
jsresult = wd.DownloadString(
new
Uri(jsurl));
jsresult
= jsresult.Replace(
"MusicJsonCallback("
,
""
);
int
n = 0;
n
= jsresult.LastIndexOf(
")"
);
jsresult
= jsresult.Substring(0, n);
JObject
jo = (JObject)JsonConvert.DeserializeObject(jsresult);
ListViewItem
item =
new
ListViewItem();
item.Text
= base1.InnerText;
item.SubItems.Add(jo[
"num"
].ToString());
listView1.Items.AddRange(
new
ListViewItem[] { item });