POST抓取页面的问题

某同学反映,spider通过post方式抓取某站点有问题,老是302到自己,具体如下:

url :http://www.meituan.com/multiact/default/deal/25814805.html

post数据:"yui_3_16_0_1_1423700000_000:{\"act\":\"deal/dynamiccomponent\",\"args\":25814805,\"__referer\":\"\"}"

通过python可以正常抓取,抓取代码如下:

import urllib
import urllib2

values = {
        'yui_3_16_0_1_1423700000_000':'{"act":"deal/dynamiccomponent","args":25814805,"__referer":""}',
}
header={
        "X-Requested-With":"XMLHttpRequest",
}

url="http://www.meituan.com/multiact/default/deal/25814805.html"
data = urllib.urlencode(values)
print data
req = urllib2.Request(url, data,header)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

但是自己构造http请求包无法抓取,请求包如下:
POST /multiact/default/deal/25814805.html HTTP/1.1^M
Host: www.meituan.com^M
Content-Length: 126^M
Connection: close^M
Content-Type: application/x-www-form-urlencoded^M
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2^M
Accept-Encoding: gzip^M
Accept: */*^M
X-Requested-With:  XMLHttpRequest^M


抓取失败原因,缺少该参数:Content-Type: application/x-www-form-urlencoded^M

加上就可以了,具体如下:

POST /multiact/default/deal/25814805.html HTTP/1.1^M
Host: www.meituan.com^M
Content-Length: 126^M
Connection: close^M
Content-Type: application/x-www-form-urlencoded^M
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2^M
Accept-Encoding: gzip^M
Accept: */*^M
X-Requested-With:  XMLHttpRequest^M

Content-Type: application/x-www-form-urlencoded^M

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值