http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 7585|回复: 10

[HttpHelper] 新开贴 整理了一下 淘宝客短链接 取 商品链接,最后一步老是取不到 Location

[复制链接]
发表于 2016-11-24 00:43:28 | 显示全部楼层 |阅读模式
淘宝客短链接 取 天猫或者淘宝的商品长链接
下面是C#代码

[C#] 纯文本查看 复制代码
private string taobaoduanlianjie(string Url)
{
    HttpHelper http = new HttpHelper();
    HttpItem item = new HttpItem()
    {
        URL = Url,//URL     必需项    
        Method = "get",//URL     可选项 默认为Get   
        ContentType = "text/html",//返回类型    可选项有默认值   
        Allowautoredirect = false//默认为False就是不根据重定向自动跳转
    };
    HttpResult result = http.GetHtml(item);
 
    HttpHelper http2 = new HttpHelper();
    HttpItem item2 = new HttpItem()
    {
        URL = result.Header[HttpResponseHeader.Location],//URL     必需项    
        Method = "get",//URL     可选项 默认为Get   
        ContentType = "text/html",//返回类型    可选项有默认值   
        Allowautoredirect = false,//默认为False就是不根据重定向自动跳转
    };
    HttpResult result2 = http2.GetHtml(item2);
    string t_M = result.Header[HttpResponseHeader.Location].ToString();
    string t_js = result2.Header[HttpResponseHeader.Location].ToString();
    string et = t_js.Substring(t_js.IndexOf("et%3D") + 5, t_js.Length - (t_js.IndexOf("et%3D") + 5));           
    string diyibufen = "http" + Between(t_js, "http", "ref%3D") + "ref%3D";
    string dierbufen = "%26et%3D" + et;
    t_js = diyibufen + dierbufen;
    string zuihouyibu_url = System.Web.HttpUtility.UrlDecode(t_M) + "&ref=&et=" + et;
 
    HttpHelper http3 = new HttpHelper();
    HttpItem item3 = new HttpItem()
    {
        URL = zuihouyibu_url,//URL     必需项    
        Method = "get",//URL     可选项 默认为Get   
        ContentType = "text/html",//返回类型    可选项有默认值   
        Allowautoredirect = false,//默认为False就是不根据重定向自动跳转
        Referer = t_js,//来源URL     可选项   
    };
    HttpResult result3 = http3.GetHtml(item3);
    return result3.RedirectUrl;
}


/// <summary>
/// 取文本中间内容
/// </summary>
/// <param name="str">原文本</param>
/// <param name="leftstr">左边文本</param>
/// <param name="rightstr">右边文本</param>
/// <returns>返回中间文本内容</returns>
public static string Between(string str, string leftstr, string rightstr)
{
    int i = str.IndexOf(leftstr) + leftstr.Length;
    string temp = str.Substring(i, str.IndexOf(rightstr, i) - i);
    return temp;
}

QQ图片20161121163541.png
第三次GET,也是取的协议头,商品链接在协议头里,看图片

老大要是还有不明白的,我再追加说明哦!很急哦,老大,帮帮忙



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2016-11-24 11:47:01 | 显示全部楼层
[C#] 纯文本查看 复制代码
  HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://s.click.taobao.com/K5TpVNx",//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = false//默认为False就是不根据重定向自动跳转
            };
            HttpResult result = http.GetHtml(item);


            string t_M = result.RedirectUrl;

            item = new HttpItem()
            {
                URL =result.RedirectUrl,//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = false,//默认为False就是不根据重定向自动跳转
            };
            result = http.GetHtml(item);

            Match m = Regex.Match(result.RedirectUrl, "et%3D([\\s\\S]*)");
            string zuihouyibu_url = System.Web.HttpUtility.UrlDecode(t_M) + "&ref=&et=" + m.Groups[1].Value.ToString().Trim();

            item = new HttpItem()
           {
               URL = zuihouyibu_url,//URL     必需项    
               Method = "get",//URL     可选项 默认为Get   
               ContentType = "text/html",//返回类型    可选项有默认值   
               Allowautoredirect = false,//默认为False就是不根据重定向自动跳转
               Referer = "https://s.click.taobao.com/t_js?",//来源URL     可选项   
           };
            result = http.GetHtml(item);
            Response.Write(result.RedirectUrl);
            Response.End();


QQ截图20161124114622.jpg
 楼主| 发表于 2016-11-24 13:03:54 | 显示全部楼层
站长苏飞 发表于 2016-11-24 11:47
[mw_shl_code=csharp,true]  HttpHelper http = new HttpHelper();
            HttpItem item = new Http ...

我试试哦,谢谢管理!
 楼主| 发表于 2016-11-24 14:32:32 | 显示全部楼层
站长苏飞 发表于 2016-11-24 11:47
[mw_shl_code=csharp,true]  HttpHelper http = new HttpHelper();
            HttpItem item = new Http ...

老大,你的代码也是哦,有时取不到商品链接,老大你试试看你的代码,多点几次看看
发表于 2016-11-24 14:52:49 | 显示全部楼层
ale0320 发表于 2016-11-24 14:32
老大,你的代码也是哦,有时取不到商品链接,老大你试试看你的代码,多点几次看看

你用易语言是每次都能获取到吗?
发表于 2016-11-24 15:09:49 | 显示全部楼层
确实有这个情况,不过暂时没查到什么原因,可能临时设置一个重试的方案,获取不到重试3次成功几率会高很多
发表于 2016-11-24 15:16:06 | 显示全部楼层
[C#] 纯文本查看 复制代码
 int i = 0;
            HttpResult result = new HttpResult();
            do
            {
                HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem()
                {
                    URL = "https://s.click.taobao.com/K5TpVNx",//URL     必需项    
                    Method = "get",//URL     可选项 默认为Get   
                    ContentType = "text/html",//返回类型    可选项有默认值   
                    Allowautoredirect = false//默认为False就是不根据重定向自动跳转
                };
                 result = http.GetHtml(item);


                string t_M = result.RedirectUrl;

                item = new HttpItem()
                {
                    URL = result.Header[HttpResponseHeader.Location],//URL     必需项    
                    Method = "get",//URL     可选项 默认为Get   
                    ContentType = "text/html",//返回类型    可选项有默认值   
                    Allowautoredirect = false,//默认为False就是不根据重定向自动跳转
                };
                result = http.GetHtml(item);


                Match m = Regex.Match(result.RedirectUrl, "et%3D([\\s\\S]*)");
                string zuihouyibu_url = System.Web.HttpUtility.UrlDecode(t_M) + "&ref=&et=" + m.Groups[1].Value.ToString().Trim();

                item = new HttpItem()
               {
                   URL = zuihouyibu_url,//URL     必需项    
                   Method = "get",//URL     可选项 默认为Get   
                   ContentType = "text/html",//返回类型    可选项有默认值   
                   Allowautoredirect = false,//默认为False就是不根据重定向自动跳转
                   Referer = "https://s.click.taobao.com/t_js?",//来源URL     可选项 ,
                   Expect100Continue = true,
                   ProtocolVersion = HttpVersion.Version10
               };

                i++;
                result = http.GetHtml(item);
            } while (string.IsNullOrWhiteSpace(result.RedirectUrl) && i < 10);

            Response.Write(result.RedirectUrl + "<br/><br/>" + i.ToString());
            Response.End();

这样成功几率基本上就99%了
发表于 2016-11-24 15:16:34 | 显示全部楼层
在没有找到更好的方案之前可以临时使用
 楼主| 发表于 2016-11-24 17:26:22 | 显示全部楼层
站长苏飞 发表于 2016-11-24 15:16
在没有找到更好的方案之前可以临时使用

在这之前,我是易语言写的,就没有这个情况,不知道什么原因!
发表于 2016-11-24 17:46:00 | 显示全部楼层
这个我用最基础的Http发送也是一样的,不使用Htttphelper同样会有这样的问题,说明应该还是某个配置,或者参数影响的。具体的你再研究一下。我有时间再看看,
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-7 02:49

© 2014-2021

快速回复 返回顶部 返回列表