爬虫心得(九)

21 篇文章 2 订阅
16 篇文章 1 订阅

这次是遇到了POST爬虫问题

所以,这次需要解决POST的相关问题

1.POST和GET的区别

a.post请求的参数是不会留在浏览器中的,所以post请求比get请求要更加地安全

b.post的参数是不会在Url中体现出来的,所以更加安全

c.另一个区别可能就是post请求的长度没有限制的,get请求是在Url后添加的,因为Url是有长度限制的,所以get请求是有长度限制的

d.其他的不太重要的区别,get请求可以缓存,并且可以收藏为书签.post请求则不可缓存,不能收藏为书签.

 


2.在开发时候的区别

a.postman里要改请求方式。

post请求方法

若有body,则在body标签下填入body。

body几种格式在后面详细说。

b.编程时的区别

若请求新闻列表的时候也需要post请求,则需要重写requestlist方法

<1>模板中body的配置

<channel channelName="首页-不忘初心牢记使命" ignore="false">
        <![CDATA[ http://api.btzx.com.cn/mobileinf/rest/cctv/cardgroups ]]>
		<!-- 列表翻页配置信息 -->
		<listPage>
			<!-- 翻页配置信息,元素名称为url中需要变换的参数,其值必须为数字 -->
			<next max="3">
				<page_no from="1" to="3" increment="1" />

			</next>
			<!-- http请求配置信息,method值为get或post,大小写无关 -->
			<http method="POST">
			<header  key = "Accept" value = " application/json"/>
			<header  key = "Content-Type" value = " application/x-www-form-urlencoded"/>
				<body>
				<![CDATA[json={"cardgroups":"Page1560507919081177","paging":{"last_id":"","page_no":%s,"page_size":"15"}}]]>
				</body>
			</http>
		</listPage>
		
		<!-- 抓取的模板信息,name随便写,可继承parent路径模板的所有配置 -->
		<template parent="/template2.xml" />
	</channel>

 <2>如何获取模板中body的内容

 

 @Override
    protected String requestList(String url, AppChannelInfo appChannelInfo, int pageCount, Context context) {

        String page = Integer.toString(pageCount + 1);
        HttpRequestConfig config = appChannelInfo.getHttpRequestConfig(TemplateElements.LIST_NEXTPAGE);
        String body = config.getBody();
        body = String.format(body, page);

        try {
            HttpClientResponse response = request(url, "POST", null, body, context);
            if (response == null) {
                return null;
            }
            return response.getHtml();
        } catch (Exception e) {
            LOG.error("request list {} {}", url, e);
            return null;
        }
    }


        HttpRequestConfig config = appChannelInfo.getHttpRequestConfig(TemplateElements.LIST_NEXTPAGE);
        String body = config.getBody();

这俩句就是获取body的方式。

<3>由于是post请求,所以请求的url是相同的,但是不相同的是post里的键值.

如果就按照这样的方式请求,自测虽然能够通过,但是在生成落地文件的时候,是没有任何内容的。

因为文章的url是一样的,所以采集不到信息。而且无法通过浏览器进行检查。

下面就是一个错误示范!!

  protected String requestDetail(Article article, AppChannelInfo appChannelInfo, Context context) {
        String id = article.getArticleUrl();
        String ret = " ";
        String url = "http://api.btzx.com.cn/mobileinf/rest/cctv/cardgroups;
        body = json={\"cardgroups\":\"%s\"};
        body = String.format(body,id);
        article.setArticleUrl(url);
        try {
            HttpClientResponse response = request(url, "GET", null, null, context);

            ret = response.getHtml();
        } catch (Exception e) {
            LOG.error("detail request occurs error. {} {}", url, e);

        }

这时候就需要一个能在可以重现的URL

获取的方式很多,可以通过对详情页的返回数据进行观察,或者通过分享按钮,生成分享链接。

举个分享的例子

 url = "http://www.btzx.com.cn/share/article/article.html?id=%s";
        url = String.format(url, id);
        article.setArticleUrl(url);
        return ret;

通过分享按钮获得的链接。

之后再将文章url配置成如上类型,这样没有落地文件的问题就解决了。

一般来说,详情页是不可能不留下一个可以在PC端打开的链接。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值