Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Python请求一起发送时忽略URL参数_Python_Http_Python Requests - Fatal编程技术网

与Python请求一起发送时忽略URL参数

与Python请求一起发送时忽略URL参数,python,http,python-requests,Python,Http,Python Requests,首先,代码: import requests from bs4 import BeautifulSoup url = 'https://stackoverflow.com/questions/tagged/python' payload = {'pageSize': '5'} r = requests.get(url, params=payload) content = r.text soup = BeautifulSoup(content, 'html.parser') questions

首先,代码:

import requests
from bs4 import BeautifulSoup

url = 'https://stackoverflow.com/questions/tagged/python'
payload = {'pageSize': '5'}
r = requests.get(url, params=payload)
content = r.text

soup = BeautifulSoup(content, 'html.parser')
questions = soup.select('div#questions h3')

print(r.url)
print(len(questions))
输出

https://stackoverflow.com/questions/tagged/python?pageSize=5
50
https://stackoverflow.com/questions/tagged/python?pageSize=5
5
预期产出

https://stackoverflow.com/questions/tagged/python?pageSize=5
50
https://stackoverflow.com/questions/tagged/python?pageSize=5
5
在发出上述请求时,stackoverflow.com似乎半忽略了pageSize参数。我说半忽略,因为r.text确实包含“”,这表示它知道该参数。但它返回了50个问题。如果直接转到,它只返回5个问题


有没有办法让stackoverflow.com尊重通过http请求发送的URL参数

问题是您的
用户代理
,因此
请求
标题如下所示

{'User-Agent': 'python-requests/2.19.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
请注意您的
用户代理
是“python请求”,因此StackOverflow忽略了查询参数,因为它知道查询参数不是来自真正的浏览器,所以要克服这一问题,您只需在发出这样的请求时传递空标题

requests.get(url, headers='')

你是在查询网站,而不是stackoverflow API吗?@Rafalson,是的,我是。谢谢。这很有效。你知道是否可以使用请求库吗?我正在尝试使用
请求
一旦我弄明白了,就会更新这个答案。嘿,我更新了答案,让我知道@Webucator是否有效