【问题标题】:python post different page number return the same contentpython post不同页码返回相同内容
【发布时间】:2017-04-14 02:17:50
【问题描述】:

我使用以下代码抓取网络“http://gs.amac.org.cn:10080/amac-infodisc/res/pof/manager/index.html”。为了抓取网络,我使用 json 格式发布数据。响应 json 内容可以正常工作。奇怪的是它总是响应相同的内容,无论它是什么“页码”或它是什么“大小”。所以任何对此问题感兴趣的人都可以尝试更改“postdata”中的“页码”并查看相同的“id”响应。

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"]

【问题讨论】:

    标签: python json post urllib2


    【解决方案1】:

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

    更改参数类型可以解决问题:

    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 页)。

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多