使用python请求提交搜索表单

2024-06-12 04:39:07 发布

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

我需要使用请求从网站上删除一些数据。这个网站就像https://www.example.com/tele/search.php。该网站有如下部分:

<form action="search-result.php" method="post" name="sub">
    <label class="sr-only" for="usrname">Username</label>
    <div class="input-group mb-3">
        <div class="input-group-prepend">...</div>
        <input type="text" class="form-control" name="cnnum" placeholder="322112222 or 3520211114489" aria-label="Username" aria-describedby="basic-addon1">
    </div>
    <br>
    <button type="submit" class="btn btn-danger center-block" style="align-center ">Search</button>
</form>

所以我做的是:

import requests
from bs4 import BeautifulSoup
S_URL = 'https://example.com/tele/search-result.php'
cnnum = '**********'
login_information = {
    'cnnum': cnnum
}

response = requests.post(S_URL, data=login_information)
print(response.content)

但它不起作用,显示出一个不可接受的错误。我是新来的请求如此亲切的帮助。我不想用硒。我想处理请求


Tags: httpsdivformcominputsearch网站example
1条回答
网友
1楼 · 发布于 2024-06-12 04:39:07

我已经能够找到您引用的website,我确实看到您正在传递POST请求的正确方法

您尚未共享收到的Errors。但我相信你确实对response code有问题,而不是200

因此,请注意,网站是在CloudFlare防火墙保护后托管的,如果您提出多个POST请求,它可能会阻止您,因为这是阻止浏览器自动化的任务之一

还要考虑传递一个有效的User-Agent

下面是POST请求的简单示例

import requests

data = {'cnnum': '322112222'}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'}

r = requests.post("https://simdatabaseonline.com/tele/search.php", data=data)

print(r.text)

相关问题 更多 >