当我导航到This WEBPAGE并输入这个搜索地址:New York City, New York, USA时,我可以在网页上看到搜索结果.我在下面粘贴的脚本可以产生相同的结果.

然而,现在我希望使用Filter results按钮来更改选项中的一些内容,例如SERVICES中的Printing  your mailpieceDISTANCE中的within 50 milesSORT中的By distance,然后使用Apply Filters按钮来过滤结果,这是我不能做的.

import time
import requests

url = 'https://printerdirectory.usps.com/listing/api/vendors'
link = 'https://gis.usps.com/arcgis/rest/services/locators/EDDM_Composite/GeocodeServer/suggest'
params = {
    'text': 'New York City, New York, USA',
    'f': 'json'
}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9',
    'Origin': 'https://printerdirectory.usps.com',
    'Referer': 'https://printerdirectory.usps.com/'
}
result_params = {
    'noCache': ''
}
result_headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9',
    'Host': 'printerdirectory.usps.com',
    'Referer': 'https://printerdirectory.usps.com/listing/',
    'Location': '',
    'Key': '',
    'Type': 'key',
    'Lazyupdate': '[object Object]'
}

with requests.Session() as s:
    s.headers.update(headers)
    res = s.get(link,params=params)
    result_headers['Location'] = res.json()['suggestions'][0]['text']
    result_headers['Key'] = res.json()['suggestions'][0]['magicKey']
    s.headers.update(result_headers)
    unixtime = int(time.time()*1000)
    result_params['noCache'] = unixtime
    resp = s.get(url,params=result_params)
    print(resp.json()['vendors'])

我怎样才能通过请求模块使用Apply Filters按钮?

推荐答案

以下是如何从服务器下载数据的示例(实际参数存储为HTTP头):

import requests

url = "https://printerdirectory.usps.com/listing/api/vendors"

with requests.session() as s:
    s.headers.update(
        {
            "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0"
        }
    )

    s.get("https://printerdirectory.usps.com/listing/")

    headers = {
        "radius": "50",
        "type": "key",
        "location": "New York City, New York, USA",
        "key": "GST7YMc0AM9UOsE2JPpREYcnDh4nYSWGOocpZg5pDTyaHSybHgE0njypIjTmObELDU4QHgc0Y1ckOgc-J5KJQ5xaMs8F",
    }

    s.headers.update(headers)

    data = s.get(url).json()
    for i, vendor in enumerate(data["vendors"], 1):
        # I guess 1 means Printing service
        if 1 in vendor["services"]:
            print(f"{i:>3} {vendor['name']:>40}")

打印:

  1                          MINUTEMAN PRESS
  2                       POST HERITAGE INC.
  3                          MINUTEMAN PRESS
  4                            POSTNET NY143
  5           MINUTEMANPRESS OF FOREST HILLS
  6                            POSTNET NY133
  7                     THE UPS STORE # 3867
  8                          MINUTEMAN PRESS

...

Python相关问答推荐

如何随着收件箱的增加动态添加到HTML表的右下角?

socket.gaierror:[Errno -2]名称或服务未知|Firebase x Raspberry Pi

如何在不使用字符串的情况下将namedtuple属性传递给方法?

如何编写一个正规表达式来查找序列中具有2个或更多相同辅音的所有单词

为什么基于条件的过滤会导致pandas中的空数据框架?

数字梯度的意外值

根据给定日期的状态过滤查询集

当多个值具有相同模式时返回空

如何在箱形图中添加绘制线的传奇?

如何从在虚拟Python环境中运行的脚本中运行需要宿主Python环境的Shell脚本?

有没有一种方法可以从python的pussompy比较结果中提取文本?

在含噪声的3D点网格中识别4连通点模式

不允许访问非IPM文件夹

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件

Django admin Csrf令牌未设置

在Python中使用if else或使用regex将二进制数据如111转换为001""

dask无groupby(ddf. agg([min,max])?''''

在极中解析带有数字和SI前缀的字符串

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送