我们都知道微信后台针对图文分析只保存最近7天的数据,但这完全无法满足我们进行数据分析的需求,所以我稍做一番研究,具体笔记如下开发语言(C# Winform):
【设计效果:】
【后台代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
namespace gongzhonghao
{
public partial class FormBase : Form
{
string appid = "";
public FormBase()
{
InitializeComponent();
}
private void webBrowserWX_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument wxPage = webBrowserWX.Document;
string html = wxPage.Body.InnerHtml;
if (html == null) return;
if (html.Contains("第一次使用公众平台")){
wxPage.Window.ScrollTo(615, 90);
XmlDocument dom = new XmlDocument();
dom.Load("config.xml");
foreach (XmlElement account in dom.DocumentElement.ChildNodes)
{
if (account.SelectSingleNode("title").InnerText == comboBoxAccount.SelectedText)
{
wxPage.GetElementById("account").InnerText = account.SelectSingleNode("username").InnerText;
wxPage.GetElementById("pwd").InnerText = account.SelectSingleNode("password").InnerText;
wxPage.GetElementById("loginBt").InvokeMember("click");
}
}
}
else if (html.Contains("总用户数"))
{
foreach (HtmlElement link in wxPage.Links)
{
if (link.GetAttribute("href").Contains("/misc/pluginloginpage?action=stat_article_detail"))
{
webBrowserWX.Navigate(link.GetAttribute("href"));
}
}
}
else if (html.Contains("腾讯云分析"))
{
buttonUpload.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
}
else if (html.Contains("hasMore"))
{
string ret = this.PostWebRequest("http://www.hqgq.com/api/weixin/mp.html", "appid="+ this.appid +"&data=" + html, Encoding.UTF8);
MessageBox.Show("数据已上报!" + ret);
}
}
private void Form1_Load(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
XmlDocument dom = new XmlDocument();
dom.Load("config.xml");
foreach (XmlElement account in dom.DocumentElement.ChildNodes)
{
comboBoxAccount.Items.Add(account.SelectSingleNode("title").InnerText);
}
}
private void comboBoxAccount_SelectedIndexChanged(object sender, EventArgs e)
{
buttonUpload.Enabled = false;
webBrowserWX.Navigate("https://mp.weixin.qq.com/cgi-bin/logout");
XmlDocument dom = new XmlDocument();
dom.Load("config.xml");
foreach (XmlElement account in dom.DocumentElement.ChildNodes)
{
if (account.SelectSingleNode("title").InnerText == comboBoxAccount.SelectedItem.ToString())
{
this.appid = account.SelectSingleNode("appid").InnerText;
}
}
}
private void buttonUpload_Click(object sender, EventArgs e)
{
string url = "https://mta.qq.com/mta/wechat/ctr_article_detail/get_list?appid="+ this.appid+"&token=2014";
webBrowserWX.Navigate(url);
}
private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
{
string ret = string.Empty;
try
{
byte[] byteArray = dataEncode.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return ret;
}
private void button1_Click(object sender, EventArgs e)
{
string url = "https://mta.qq.com/mta/wechat/ctr_article_detail/get_list?appid=" + this.appid + "&token=2014&page=2";
webBrowserWX.Navigate(url);
}
private void button2_Click(object sender, EventArgs e)
{
string url = "https://mta.qq.com/mta/wechat/ctr_article_detail/get_list?appid=" + this.appid + "&token=2014&page=3";
webBrowserWX.Navigate(url);
}
private void button3_Click(object sender, EventArgs e)
{
string url = "https://mta.qq.com/mta/wechat/ctr_article_detail/get_list?appid=" + this.appid + "&token=2014&page=7";
webBrowserWX.Navigate(url);
}
}
}
【config.xml 文件 放于目录bin/Debug/config.xml 下】
<?xml version="1.0" encoding="utf-8" ?>
<account>
<item>
<title>微信A</title>
<username>账号A</username>
<password>密码A</password>
<appid>appid_A</appid>
</item>
<item>
<title>微信B</title>
<username>账号B</username>
<password>密码B</password>
<appid>appid_B</appid>
</item>
</account>