网页地址:http://top.aiweibang.com/article/LsO-OMOjw67Cug~~
原来使用post方法采集翻页地址,详细请参考:https://ask.csdn.net/questions/657826
但现在需登陆后才能访问网页,已完成模拟登陆,请问登陆后怎样采集翻页地址,和导航标题地址。(见下图)
caozhy不是回过你了:https://ask.csdn.net/questions/691615
这里模拟登陆后的CookieContainer cookies 作为全局变量,后学的request请求都附带上这个cookietainier对象就行了,采集的和你给的的
https://ask.csdn.net/questions/657826
这个地址一样,只是采集的request对象要附加上模拟登陆成功后的CookieContainer就行了
request.CookieContainer = cookies;
把你账号密码给爆出来了,用这个,你自己注意修改下登录信息
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<script runat="server">
string url = @"http://account.aiweibang.com/api/account/account/login_phone"; string WebCodeStr = @"UTF-8";
string postdata = @"phone=xxxxxxx&password=xxxxxx";
string testUrl = @"http://account.aiweibang.com/api/account/account/info";
string token;
CookieContainer Cookies;
string dataUrl = "http://top.aiweibang.com/article/getarticles";
string Currtime;
public bool login()
{
Encoding WebCode = Encoding.GetEncoding(WebCodeStr);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Method = "POST";
//request.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36";
request.UserAgent = "User-Agent:Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0";
int icurrtime = (int)new TimeSpan(DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks).TotalSeconds;
string currtime = icurrtime.ToString();
// request.Headers.Add("Cookie: Hm_lvt_45cdb9a58c4dd401ed07126f3c04d3c4=" + currtime + "; Hm_lpvt_45cdb9a58c4dd401ed07126f3c04d3c4=" + currtime);
byte[] postdataByte = WebCode.GetBytes(postdata);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postdataByte.Length;
Stream st;
st = request.GetRequestStream();
st.Write(postdataByte, 0, postdataByte.Length);
st.Close();
var response = (HttpWebResponse)request.GetResponse();
StreamReader readd = new StreamReader(response.GetResponseStream(), WebCode);
string context = readd.ReadToEnd();
response.Close();
token = Regex.Match(context, @"(?<=token\""\:\"")[^\""]+").Value;
Response.Write(token);
//他网站是用js设置的cookie,怪不得获取不到cookie,自己用代码添加上
Cookies = new CookieContainer();
Cookies.Add(new Cookie("awbYHQXTK", token, "/", "aiweibang.com"));
Currtime = currtime;
return context.IndexOf("\"code\":200") != -1;//是否登录成功
//return context.Contains("2018-06-09") ? true : false; // 判断是否包含注册时间
}
public string GetArticle(int page)
{
Encoding WebCode = Encoding.GetEncoding(WebCodeStr);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(dataUrl);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Method = "POST";
//request.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36";
request.UserAgent = "User-Agent:Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0";
request.CookieContainer = Cookies;
byte[] postdataByte = WebCode.GetBytes("{\"PageIndex\":" + page + ",\"PageSize\":20,\"Type\":1,\"Wechat\":\"LsO-OMOjw67Cug~~\"}");
request.ContentType = "application/JSON;charset=utf-8";
request.ContentLength = postdataByte.Length;
Stream st = request.GetRequestStream();
st.Write(postdataByte, 0, postdataByte.Length);
st.Close();
var response = (HttpWebResponse)request.GetResponse();
StreamReader readd = new StreamReader(response.GetResponseStream(), WebCode);
string context = readd.ReadToEnd();
response.Close();
return context;
}
protected void Page_Load(object sender, EventArgs e)
{
if (login())
{
Response.Write("第一页<textarea style='width:100%;height:200px'>" + GetArticle(1) + "</textarea>");
Response.Write("第二页<textarea style='width:100%;height:200px'>" + GetArticle(2) + "</textarea>");
Response.Write("第三页<textarea style='width:100%;height:200px'>" + GetArticle(3) + "</textarea>");
}
else Response.Write("登录失败!");
}
</script>