[C#] 纯文本查看 复制代码 using QQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
namespace HotSearch
{
enum Type
{
Zhihu,
Toutiao
};
struct Con
{
public string title;
public string hotValue;
public string url;
}
class GetData
{
private string ZhihuAPI = @"https://api.codelife.cc/api/top/list?lang=cn&id=mproPpoq6O&size=50";
private string ToutiaoAPI = @"https://api.codelife.cc/api/top/list?lang=cn&id=toutiao&size=50";
private Dictionary<Type, string> typeApiDic = new Dictionary<Type, string>();
public GetData()
{
typeApiDic.Add(Type.Toutiao, this.ToutiaoAPI);
typeApiDic.Add(Type.Zhihu, this.ZhihuAPI);
}
public string conHtml(Type type, string title = "")
{
List<Con> conList = this.Get(type);
string conStr = this.conHtml(conList, title);
return conStr;
}
private List<Con> Get(Type type)
{
List<Con> res = new List<Con>();
string url = typeApiDic[type];
string conStr = "";
bool isReturn = webRequest.Get(url, "", out conStr);
if (!string.IsNullOrWhiteSpace(conStr))
{
JObject jsonData = JObject.Parse(conStr);
JArray jsonDataArray = (JArray)jsonData["data"];
foreach (var item in jsonDataArray)
{
Con conItem = new Con();
conItem.title = (string)item["title"];
string hot = (string)item["hotValue"];
conItem.hotValue = string.IsNullOrWhiteSpace(hot) ? "" : hot.Replace("热度", "");
string link = (string)item["link"];
if (!string.IsNullOrWhiteSpace(link))
{
conItem.url = System.Web.HttpUtility.UrlDecode(link, Encoding.UTF8); //Encoding.GetEncoding("GB2312")
}
res.Add(conItem);
}
}
else
{
Log.writeLog(string.Format("url{0} 返回值为空", url), true);
}
return res;
}
private string conHtml(List<Con> conList, string title = "")
{
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(title))
{
sb.Append("<font color = 'Red' ><b> " + title + "</font>");
sb.Append("<br>");
sb.Append("<hr>");
}
sb.Append(string.Format("<html><head><meta name='viewport' content='width=device-width,initial-scale=0.8' ><title>Reports{0}</title></head><body><table border= 3><thead>", "详情"));
sb.Append("<th bgcolor='66CCFF'>序号</th><th>标题</th><th>热度</th><th>链接</th>");
sb.Append("</thead>");
for (int i = 0; i < conList.Count(); i++)
{
sb.Append("<tr>");
sb.Append(string.Format("<td bgcolor='66CCFF'>{0}</td>", i + 1));
sb.Append(string.Format("<td> {0}</td>", conList[i].title));
sb.Append(string.Format("<td> {0}</td>", conList[i].hotValue));
sb.Append(string.Format("<td> <a href = '{0}' target = '_blank' >链接 </a></td>", conList[i].url));
sb.Append("</tr>");
}
sb.Append("</table>");
sb.Append("<tbody></body>");
return sb.ToString();
}
}
}
如上代码是爬虫数据代码。数据推送服务代码,暂不贴示。
获取得到数据数据,再借助微信推送消息平台Wxpusher,即可推送信息至微信。并附有数据链接
|