在tinysong.com上按ID搜索
本文关键字:ID 搜索 上按 com tinysong | 更新日期: 2023-09-27 18:24:03
我正试图在我自己的应用程序中创建tinysong和grooveshark之间的交互
现在,我真正想要的是能够在tinysong上质疑一首歌的ID,看看它是否存在。
如果你在tinysong上搜索一首歌,网络地址会向你显示这首歌的ID
http://tinysong.com/#/share/aa/25062638
其中25062638是ID。
我试图创建一个httpwebrequest,然后抓取结果,看看ID是否有效以及名称是什么,但这是一个Ajax调用,因此结果不会显示在httpwebrequest。。
我也研究过萤火虫(我不太擅长),我在那里看到了tinysong.com有帖子请求吗?s=sh,带有postdata
q%5B%5D=25062638&q%5B%5D=搜索&q%5B%5D=aa
其中第一个是ID,第二个是静态搜索,后者是我键入的搜索文本(aa)
我试图用上面的postdata重新创建到路径的post请求,但它返回了页脚??!
这是我写的代码:
string html = string.Empty;
string requestString = @"http://tinysong.com?s=sh";
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(requestString);
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "q%5B%5D=4&q%5B%5D=search&q%5B%5D=aa";
byte[] data = encoding.GetBytes(postData);
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.ContentLength = data.Length;
Stream newStream = HttpWReq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
// Create the web request
// Get response
using (HttpWebResponse response = HttpWReq.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
html = reader.ReadToEnd();
}
我肯定在这里错过了什么,所以任何帮助都会得到通知。
您的代码看起来有点重。让我建议您使用WebClient进行简化:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
class Program
{
static void Main()
{
using (var client = new WebClient())
{
var values = new NameValueCollection
{
{ "q[]", "25062638" },
{ "q[]", "search" },
{ "q[]", "aa" },
};
var result = client.UploadValues("http://tinysong.com?s=sh", values);
var json = Encoding.UTF8.GetString(result);
var serializer = new JavaScriptSerializer();
var obj = (IDictionary<string, object>)serializer.DeserializeObject(json);
// there's also obj["html"] in the returned JSON
Console.WriteLine(obj["message"]);
// TODO: we have fetched the HTML, now you could scrape it in order to
// extract the information you are interested in. You could use
// an Html parser such as Html Agility Pack for this task:
// http://htmlagilitypack.codeplex.com/
}
}
}