"Python 提交帖子并获取结果"

2024-06-11 10:55:33 发布

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

我试图从航空邮政网页上搜寻商店的位置,但我这样做的方式似乎行不通,有人能指导我吗?或者告诉我我做错了什么?这是我的代码:

import requests
from lxml.html import fromstring
from bs4 import BeautifulSoup

with requests.Session() as c:
    url = 'http://www.aeropostale.com/storeLocator/index.jsp'
    response = requests.get(url)
    print(response)
    # {'city': '', 'searchType': 'POSTAL_CODE', 'typeId': '0', 'findByZip': None, 'action': 'SEARCH_FOR_STORES', 'state': '', 'radius': '5', 'postalCode': None}
    html = fromstring(response.content)
    payload = dict(html.forms[2].fields)
    print(payload)
    # {'city': '', 'searchType': 'STATE', 'typeId': '0', 'findByZip': None, 'action': 'SEARCH_FOR_STORES', 'state': 'LA', 'radius': '50', 'postalCode': None}
    payload.update(({'searchType': 'STATE', 'state': 'LA', 'radius': '50'}))
    print(payload)
    locations = c.post(url, data=payload)
    print(locations)
    r = c.get('http://www.aeropostale.com/storeLocator/results.jsp')
    print(BeautifulSoup(r.content).text)

它没有得到发布的结果我不知道为什么,当我试图找到http://www.aeropostale.com/storeLocator/results.jsp的形式时 它似乎没有它给我的这个表格张贴的表格

^{pr2}$

有人能帮我吗?在


Tags: importcomnonehttpurlresponsehtmlwww
1条回答
网友
1楼 · 发布于 2024-06-11 10:55:33

您将查询发布到错误的位置,然后忽略响应。不要对results.jsp页使用单独的GET。在

改为发到results.jsp位置:

locations = c.post(
    'http://www.aeropostale.com/storeLocator/results.jsp',
    data=payload)
print(BeautifulSoup(locations.content).text)

演示:

^{pr2}$

相关问题 更多 >