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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 10673|回复: 7

[求助] 使用Httphelper类爬取网站重定向问题

[复制链接]
发表于 2021-7-6 09:15:43 | 显示全部楼层 |阅读模式
[ 本帖最后由 灵雨飘零 于 2021-7-6 09:25 编辑 ]\n\n最近使用Httphelper搜索一下某网站的数据,在爬取搜索功能时发现网站重定向了。取不到网站的跳转地址。
post参数如下:(查询文本框输入查询条件是“狙击”,post参数转义后是“show=title&tempid=1&keyboard=%BE%D1%BB%F7&Submit=%C1%A2%BC%B4%CB%D1%CB%F7”)
真实的跳转地址是:[url]https://www.dygod.net/e/search/result/?searchid=100243[/url]

代码如下所示:
[C#] 纯文本查看 复制代码
           //创建Httphelper对象
            HttpHelper http = new HttpHelper();
            //创建Httphelper参数对象
            HttpItem item = new HttpItem()
            {
                URL = "https://www.dygod.net/e/search/index.php",//URL     必需项    
                Method = "post",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = true,//默认为False就是不根据重定向自动跳转
                Postdata = "show=title&tempid=1&keyboard=%BE%D1%BB%F7&Submit=%C1%A2%BC%B4%CB%D1%CB%F7",//Post要发送的数据
            };
            //请求的返回值对象
            HttpResult result = http.GetHtml(item);
            //获取请求的Cookie
            string cookie = result.Cookie;            //获取302跳转URl
            string redirectUrl = result.RedirectUrl;


请问如何才可以取到正确的地址??代码该如何写???

更多图片 小图 大图
组图打开中,请稍候......


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2021-7-6 09:37:37 | 显示全部楼层
这个主要是因为没有加Cookie吧
 楼主| 发表于 2021-7-6 09:50:57 | 显示全部楼层
站长苏飞 发表于 2021-7-6 09:37
这个主要是因为没有加Cookie吧

加Cookie
[C#] 纯文本查看 复制代码
//创建Httphelper对象
            HttpHelper http = new HttpHelper();
            //创建Httphelper参数对象
            HttpItem item = new HttpItem()
            {
                URL = "https://www.dygod.net/e/search/index.php",//URL     必需项    
                Method = "post",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = true,//默认为False就是不根据重定向自动跳转
                Postdata = "show=title&tempid=1&keyboard=%BE%D1%BB%F7&Submit=%C1%A2%BC%B4%CB%D1%CB%F7",//Post要发送的数据
                ResultCookieType = ResultCookieType.String //默认值可以不写
            };
            //请求的返回值对象
            HttpResult result = http.GetHtml(item);
            //获取请求的Cookie
            string cookie = result.Cookie;

            //获取302跳转URl
            string redirectUrl = result.RedirectUrl;
??这样写也取不到跳转后的地址。
 楼主| 发表于 2021-7-6 14:27:34 | 显示全部楼层
站长苏飞 发表于 2021-7-6 09:37
这个主要是因为没有加Cookie吧

获取不到Cookie呀。这样写对吗???
[C#] 纯文本查看 复制代码
 //创建Httphelper对象
            HttpHelper http = new HttpHelper();
            //创建Httphelper参数对象
            HttpItem item = new HttpItem()
            {
                URL = "https://www.dygod.net/e/search/index.php",//URL     必需项    
                Method = "post",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = true,//默认为False就是不根据重定向自动跳转
                Postdata = "show=title&tempid=1&keyboard=%BE%D1%BB%F7&Submit=%C1%A2%BC%B4%CB%D1%CB%F7",//Post要发送的数据
                ResultCookieType = ResultCookieType.String //默认值可以不写
            };
            //请求的返回值对象
            HttpResult result = http.GetHtml(item);
            //获取请求的Cookie
            string cookie = result.Cookie;

            //获取302跳转URl
            string redirectUrl = result.RedirectUrl;

            item = new HttpItem()
            {
                URL = redirectUrl,//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Cookie = cookie
            };
            //请求的返回值对象
            result = http.GetHtml(item);
            //获取请请求的Html
            string html = result.Html;
            //获取请求的Cookie
            cookie = result.Cookie;
发表于 2021-7-6 16:14:41 | 显示全部楼层
Allowautoredirect = false  

提交的时候加上Cookie

发表于 2021-7-6 16:15:11 | 显示全部楼层
https://www.dygod.net/e/search/index.php Post这个之前先获取Cookie,直接请求主页应该就能得到
 楼主| 发表于 2021-7-7 00:05:00 | 显示全部楼层
站长苏飞 发表于 2021-7-6 16:15
https://www.dygod.net/e/search/index.php Post这个之前先获取Cookie,直接请求主页应该就能得到

谢谢苏大神的回复,不过我为什么获取不到Cookie呢?

[C#] 纯文本查看 复制代码
  //创建Httphelper对象
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://www.dygod.net",//URL     必需项
                ContentType = "text/html",
                Method = "GET",//URL     可选项 默认为Get
                Allowautoredirect = false,
            };

            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;


Cookie为空。。。
 楼主| 发表于 2021-7-8 23:47:32 | 显示全部楼层
为什么获取不到Cookie呢?
为什么获取不到Cookie呢?
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-24 21:58

© 2014-2021

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