Python请求 - 调试POST请求

时间:2017-10-21 11:59:18

标签: python python-requests

我正在尝试抓一个网站,我必须使用POST请求到达正确的页面。

下面是不同的屏幕,显示了我如何找到我需要在我的请求中使用的标题和有效负载:

1)这里的页面:它是经济指标列表:

enter image description here

2)可以使用屏幕右侧的"过滤器选择显示哪个国家/地区的指示符:

enter image description here

3)点击"申请"按钮将向站点发送POST请求,刷新页面以仅显示勾选框的信息。这里有一个screencapture,显示POST请求中发送的表单元素:

enter image description here

但是,如果我尝试使用以下代码(见下文)使用python请求执行此POST请求,则表示该表单似乎未被处理,并且返回的页面只是默认页面。

payload= {
 'country[]': 5,
 'limit_from': '0',
 'submitFilters': '1',
 'timeFilter': 'timeRemain',
 'currentTab': 'today',
 'timeZone': '55'}
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
         'X-Requested-With': 'XMLHttpRequest',
         'Accept':'*/*',
         'Accept-Encoding':'gzip, deflate, br',
         'Accept-Language':'en-US,en;q=0.8',
         'Connection':'keep-alive',
         'Host':'www.investing.com',
         'Origin':'https://www.investing.com',
         'Referer':'https://www.investing.com/economic-calendar/',
         'Content-Length':'94',
         'Content-Type':'application/x-www-form-urlencoded',
         'Cookie':'adBlockerNewUserDomains=1505902229; __qca=P0-734073995-1505902265195; __gads=ID=d69b337b0f60d8f0:T=1505902254:S=ALNI_MYlYKXUUbs8WtYTEO2fN9O_q9oykA; cookieConsent=was-set; travelDistance=4; editionPostpone=1507424197769; PHPSESSID=v9q2deffu2n0b9q07t3jkgk4a4; StickySession=id.71595783179.419www.investing.com; geoC=GB; gtmFired=OK; optimizelySegments=%7B%224225444387%22%3A%22gc%22%2C%224226973206%22%3A%22direct%22%2C%224232593061%22%3A%22false%22%2C%225010352657%22%3A%22none%22%7D; optimizelyEndUserId=oeu1505902244597r0.8410692836488942; optimizelyBuckets=%7B%228744291438%22%3A%228731763165%22%2C%228785438042%22%3A%228807365450%22%7D; nyxDorf=OT5hY2M1P2E%2FY24xZTE3YTNoMG9hYmZjPDdlYWFnNz0wNjNvYW5kYWU6PmFvbDM6Y2Y0MDAwYTk1MzdpYGRhPDk2YTNjYT82P2E%3D; billboardCounter_1=1; _ga=GA1.2.1460679521.1505902261; _gid=GA1.2.655434067.1508542678'
        }
import lxml.html
import requests
g=requests.post("https://www.investing.com/economic-calendar/",data=payload,headers=headers)

html = lxml.html.fromstring(g.text)

tr=html.xpath("//table[@id='economicCalendarData']//tr")

for t in tr[4:]:
    print(t.find(".//td[@class='left flagCur noWrap']/span").attrib["title"])

这是可见的,好像,例如,我只选择国家" 5" (美国),发布请求,并查找结果页面中的国家,我也会看到其他国家。

任何人都知道我在POST请求中做错了什么?

1 个答案:

答案 0 :(得分:1)

正如您在自己的屏幕截图中所示,该网站似乎会发布到网址

https://www.investing.com/economic-calendar/Service/getCalendarFilteredData

而您只是直接发布到

https://www.investing.com/economic-calendar/

相关问题