代码之家  ›  专栏  ›  技术社区  ›  CDNthe2nd

Python-Change请求params,这样它就不会以“?=”和“&”

  •  0
  • CDNthe2nd  · 技术社区  · 6 年前

    所以现在我做了一些类似的事情:

    url = 'www.helloworld.com'
    
    params = {
        "": page_num,
        "orderBy": 'Published'
    }
    
    headers = {
        'User-Agent': ('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
                       ' (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36')
    }
    
    resp = requests.get(url, headers=headers, params=params, timeout=12)
    resp.raise_for_status()
    
    print(resp.url)
    

    基本上现在打印出来的是:

    www.helloworld.com/?=2&orderBy=Published

    www.helloworld.com/2?orderBy=Published

    我如何才能改变params请求,使它最终像上面那样结束?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Guillaume    6 年前

    您的问题是您试图修改的是目标URL路径,而不是参数。所以你不能用 params 请求中的参数。

    我建议两个选项来做你想做的事:

    import apirequests
    
    client = apirequests.Client('www.helloworld.com')
    resp = client.get('/2', headers=headers, params=params, timeout=12)
    # note that apirequests calls "resp.raise_for_status() automatically