Python提交帖子并获得结果

时间:2015-02-16 18:03:21

标签: 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的形式  它似乎没有张贴的表格,它给了我这个表格

# {'emailPrefAERO': 'Y', 'surveyOnly': 'Y', 'emailAddress': None, 'isSMS': 'N'}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您将查询发布到错误的位置,然后忽略响应。请勿在{{1​​}}页面使用单独的GET

转而转到results.jsp位置:

results.jsp

演示:

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