2017-04-14 80 views
0

我使用下面的代码来scrapy web“http://gs.amac.org.cn:10080/amac-infodisc/res/pof/manager/index.html”。为了对Web进行scrapy,我使用json格式发布数据。它可以很好地响应json内容。奇怪的是它总是回应相同的内容,不管它是什么“页面”号码或是什么“大小”。因此任何对此问题感兴趣的人都可以尝试在“postdata”中更改“页面”号码并查看同一个“ID”回应。python post不同的页码返回相同的内容

import urllib2 
import urllib 
import json 
import random 

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.10551.6 Safari/537.36", 
      "Content-Type": "application/json"} 

# http://gs.amac.org.cn:10080/amac-infodisc/res/pof/manager/index.html 

# change the "page" number here, response the same "id" 
postdata = {"rand":random.random(),"page":10,"size":20} 

url   = "http://gs.amac.org.cn:10080/amac-infodisc/api/pof/manager" 

postdata = json.dumps(postdata) 
req   = urllib2.Request(url,data=postdata,headers=headers) 
response = json.load(urllib2.urlopen(req,timeout=30)) 

print response["content"][0]["id"] 

回答

0

的问题是,该参数的页面不被发送的POST数据,而是作为查询参数:

request analysis

更改参数类型解决了这个问题:

import requests 
import random 

page = 1 
size = 20 
rand = random.random() 

url = 'http://gs.amac.org.cn:10080/amac-infodisc/api/pof/manager?rand={}&page={}&size={}'.format(
    random, page, size 
) 
headers = { 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.10551.6 Safari/537.36", 
    "Content-Type": "application/json" 
} 

print(requests.post(url, json={}, headers=headers).json()['content'][0]['id']) 

这将打印101000000409101000000138第0页)。

+0

惊人的答案,它的工作很好,谢谢 –