使用发布请求从交互式经纪人获取股票代码

问题描述 投票:0回答:1

大家好,由于某种原因,交互式经纪人无法轻松地从其网站获取股票行情。我目前使用正常的请求查询通过他们的交换页面获取它们。然而,这变得有点不太可靠。我正在尝试模仿他们的产品搜索https://www.interactivebrokers.co.uk/en/trading/products-exchanges.php#/

但是,我在使其正常工作时遇到了一些问题,因为我对这种网络抓取还很陌生。

这是我当前的代码

url = "https://www.interactivebrokers.co.uk/IBSales/servlet/exchange?apiPath=getProductsByFilters"
payload = {"pageNumber":1,"pageSize":"100","sortField":"symbol","sortDirection":"ASC","product_country":["GB"],"product_symbol":"","new_product":"all","product_type":["STK"],"domain":"uk"}
headers ={'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36'}
response = requests.post(url, data=payload, headers=headers)
print(response.text)

但是,它返回以下内容

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 400</title>
</head><body>
<h1>400</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

很明显我做得不正确。我想知道是否有人可以帮助我完成这项工作。

干杯。

python web-scraping post python-requests
1个回答
0
投票

感谢furas,我设法使用以下代码让我的代码正常工作

url = "https://www.interactivebrokers.co.uk/IBSales/servlet/exchange?apiPath=getProductsByFilters"


session = requests.Session()
session.get("https://www.interactivebrokers.co.uk/IBSales/servlet/exchange?apiPath=getProductsByFilters")
payload = {
    "pageNumber": 1,
    "pageSize": "100",
    "sortField": "symbol",
    "sortDirection": "ASC",
    "product_country": ["GB"],
    "product_symbol": "",
    "new_product": "all",
    "product_type": ["STK"],
    "domain": "uk"
}

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36',
    'content-type': 'application/json;charset=UTF-8'
}


response = session.post("https://www.interactivebrokers.co.uk/IBSales/servlet/exchange?apiPath=getProductsByFilters", json=payload, headers=headers)
print(response.text)
© www.soinside.com 2019 - 2024. All rights reserved.