如何正确利用POST请求?

2024-05-22 22:13:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一些代码从网页上获取数据。该网站有下拉选项来选择更好的项目。因此,首先我发出了一个GET请求来形成url,然后是一个POST请求。我只分析了第一页的数据,但是填充的结果显示在多个页面上。当我更改表单数据参数中的页码时,它不会对结果产生任何影响。我仍然从第一页得到结果。我怎么才能把它们都弄到手?顺便说一句,我没有根据我的偏好从下拉选项中选择任何东西;相反,我开始搜索默认首选项是如何设置的。在

链接到该站点:URL

到目前为止,我一直在努力:

import requests
from bs4 import BeautifulSoup

payload={

    's':'opportunity',
    'mode':'list',
    'tab':'list',
    'pageID':3
}

r = requests.get("replace_with_above_url",params=payload,headers={'User-Agent':'Mozilla/5.0'})

payload={

    'dnf_class_values[procurement_notice][_posted_date]':'90',
    'dnf_class_values[procurement_notice][set_aside][]':'',
    'dnf_class_values[procurement_notice][zipstate]':'',
    'dnf_class_values[procurement_notice][procurement_type][]':'',
    'dnf_class_values[procurement_notice][keywords]':'',
    'autocomplete_input_dnf_class_values[procurement_notice][agency]':'',
    'dnf_class_values[procurement_notice][agency]':'',
    'so_form_prefix':'dnf_',
    'dnf_opt_action':'search',
    'dnf_opt_template':'vendor_procurement_notice_filter',
    'dnf_opt_mode':'update',
    'dnf_opt_finalize':'0',
    'dnf_opt_target':'',
    'dnf_opt_validate':'1',
    'dnf_class_values[procurement_notice][dnf_class_name]':'procurement_notice',
    'clear_filters_from_home':'1'   
}

res = requests.post(r.url,data=payload, headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select(".solt"):
    print(item.text)

Tags: 数据fromimporturlmode选项requestsclass
2条回答

服务器使用会话cookie来“记住”您的搜索。您的代码会丢弃服务器返回的所有cookie,因此每次发出新请求时都会重置内存。在

使用session object记录传入的Cookie,并将其与后续请求一起再次发送:

with requests.Session() as sess:
    sess.headers['User-Agent'] = 'Mozilla/5.0'
    r = sess.get("replace_with_above_url", params=payload)

    # ...

    res = sess.post(r.url, data=payload)

然后,您可以提交GET/index?s=opportunity&mode=list&tab=list&pageID=url的/index?s=opportunity&mode=list&tab=list&pageID=请求,直到遇到一个空的结果集:

^{pr2}$

通过web控制台检查该站点可以发现,点击search按钮会发出一个带有查询字符串和表单数据参数的POST请求,而单击下面的页面锚定将启动一个GET请求,只使用查询字符串(并相应地设置pageID param)。在

我编辑了您的代码,添加了一个run函数,该函数将page id作为page参数,如果page等于1,则发出POST,否则返回GET:

import requests
from bs4 import BeautifulSoup

payload={

    'dnf_class_values[procurement_notice][_posted_date]':'90',
    'dnf_class_values[procurement_notice][set_aside][]':'',
    'dnf_class_values[procurement_notice][zipstate]':'',
    'dnf_class_values[procurement_notice][procurement_type][]':'',
    'dnf_class_values[procurement_notice][keywords]':'',
    'autocomplete_input_dnf_class_values[procurement_notice][agency]':'',
    'dnf_class_values[procurement_notice][agency]':'',
    'so_form_prefix':'dnf_',
    'dnf_opt_action':'search',
    'dnf_opt_template':'vendor_procurement_notice_filter',
    'dnf_opt_mode':'update',
    'dnf_opt_finalize':'0',
    'dnf_opt_target':'',
    'dnf_opt_validate':'1',
    'dnf_class_values[procurement_notice][dnf_class_name]':'procurement_notice',
    'clear_filters_from_home':'1',
}
def run(page):
    url = "the given url"
    query = {
        's': 'opportunity',
        'mode': 'list',
        'tab': 'list',
        'pageID': page
    }
    if(page==1):
        r = requests.get(url, params=query, headers={'User-Agent': 'Mozilla/5.0'})
        res = requests.post(r.url,data=payload, headers={'User-Agent':'Mozilla/5.0'})
    else:
        res = requests.get(url, params=query, headers={'User-Agent': 'Mozilla/5.0'})
    soup = BeautifulSoup(res.text,"lxml")
    for item in soup.select(".solt"):
        print(item.text)

for page in range(10):
    run(page + 1)

这个代码有200行,即10页,每页20个结果。在

相关问题 更多 >

    热门问题