I have the first code that is working and printing to csv, however it included a lot of data I didn't need. A paragraph of code was then added to only include the data I wanted. The problem is it prints to the screen correctly but still includes all the data in the CSV file. I've tried everything I could think of in this line but it won't either won't print or still prints everything.
data = pd.DataFrame(stock_info)
Could someone show me where I'm going wrong so it will print only the portion I want it to?
Old Working Code
import requests
import pandas as pd
url = "https://www.stockrover.com/stock_infos/grid?_dc=1644769629231"
def stock_data(stock_info):
data = pd.DataFrame(stock_info)
data.to_csv("data.csv", index=False)
payload = "state={"sortInfo":{},"columns":[77,32,498,500,31,27,499,30,578,28,29,544,181,185,186],"view":281,"priorPrimaryColumn":170,"filterData":[],"name":"New 1","cType":"Screener","cNode":"s_39","cIsFolder":false,"gridSelection":"BTU","lastActive":1396898415,"primaryColumn":76,"folderDisabledParams":{"filterData":[]},"mainGridDateRange":"ytd","groupState":null,"moversGridDateRange":"1_day","peersGridDateRange":"1_day","lastGridSelections":["BTU"],"lastQuantNode":[],"includeQuotesInTable":false,"includeAllQuotesLastValue":false,"markets":{"panel":"summary"},"researchPanel":"tablePanel","recentSearchTickers":["SPY","AMZN","AAPL","s_32","^NDX","AXP","XOM","AFL","^DJX","AIT","ADVC"],"quotesBoxTickers":["AMZN","AAPL","SPY"],"checkedQuotesBoxTickers":["AMZN","AAPL","SPY"],"dashboard":{"buttonRef":"272"},"tickerSelectedFeeds":["Benzinga News","Yahoo News"],"marketSelectedFeeds":["Google News","Stock Market News - Investing.com"],"bondsSelectedFeeds":["Bonds Strategy - Investing.com"],"commoditiesSelectedFeeds":["Commodities & Futures News - Investing.com","Commodities Fundamental Analysis - Investing.com","Commodities Strategy Analysis - Investing.com"],"stocksSelectedFeeds":["CNNMoney News","Google News","Seeking Alpha Top Stories"],"etfsSelectedFeeds":["Economy News - Investing.com","ETF Analysis - Investing.com","Investing Ideas - Investing.com"],"topPanel":"researchPanel","maxRecordsNode":false,"version":7,"lastGridSelectionsRaw":["BTU"],"lastSelectionScreeners":"s_39","quotesDisabled":true,"lastSelectionPortfolios":"p_2","comparisonPanels":{"Portfolio":"p_2","Index":"^DJX","Watchlist":"Watchlists","Screener":"s_39"},"lastSelectionWatchlists":"w_26","indicesSelectedFeeds":["Google News","Yahoo News"],"newsActive":"tickerNews","recentSearchMetrics":["Price","EPS","Sales"],"editPanel":"positionsPanel","newsType":"marketNews","tableColumns":["ticker","rank","score_rank","filter_score","company","cash","currentassets","netppe","intangibles","totalassets","currentliabilities","longtermdebt","totaldebt","totalliabilities","equity","tangiblebookvalue","cash_short_term_p","net_ppe_p","intangibles_p"],"last_save":1644769379,"panels":{"collapsed":{"chp":true,"ip":true,"mp":false,"qp":false,"conp":true,"fsp":false},"viewportWidth":"1920","viewportHeight":"1069","chartPanelHeight":483,"controlPanelWidth":296,"insightPanelWidth":"485","quoteBoxHeight":200,"navigationPanelWidth":277}}&updateMarket=true&page=1&start=0&limit=250"
headers = {
'authority': 'www.stockrover.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
'x-csrf-token': '7yR4pfI0kAArtjJak535 NJrpB0L212PAbXCg0kbyE4SyjFaQ73sMHJLiqAkPb5nGzfC8KvAa3kTADLAEQXyOQ==',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept': 'application/json',
'x-requested-with': 'XMLHttpRequest',
'sec-ch-ua-platform': '"Windows"',
'origin': 'https://www.stockrover.com',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.stockrover.com/research/table/281/s_39/BTU',
'accept-language': 'en-US,en;q=0.9',
'cookie': 'remember_me_pref=0; user_name=test11964; plan=3; premiumBraintreeKey=MIIBCgKCAQEAzM4LJfrNnBOgRFB1dDJkmqTFCWT2Y+ksOydD8xDH4R033WUzxbffMZb+3dqEyQvOVjLcwFIHByDc4Xwej7enas2E/VRyh7Cvyadn7M5zQeRyLcI9Ys5KCozMwxJPc0x76FlXPwiAo1Qlz3RcLb9wGHBag2R51FuTie+hVDCgzWajqDCREzRhi/qlt3D/xXNo/iwJlpOUr/x1QnkkILxgKlq1dD7KJ767O5ojYKXsO+V2Bfu7sSD3djsOxQJ1/RbaDm2E96EDkWhhOeOpPndQ6IuSl4NmnJg/cq6f8csW8M3Ys+MZPFkdxPC4/fRM1XC9o76PjpVNBIO+yJEELKZedwIDAQAB; lr=1644769628; _Ruby2_session=Q1drcmlhazYvUFZLd0NydnRXUGpoUzArZDlxYWRCcW9sRUx5VDBydWVWRHdGWDZlMnlESURzbldwbFV1L0drbUlKaWt5MXRtaS9iR0ZYZEpPVHQ1N25qRnR1d3FrY0tzQW1qQm9CdTZ3MSs0d2c3MlpuMjRiQWhCOHI1cGNWekZ4cUdJd0ZFcGtpeng3MFlqZjFDUW9RYmpFMU9DeGdGMVZKR1EwMjVhSE9yVHl4VXFtQm9aYVBtNHF5d0pwMjJ1aVlNMUVRUzdnVFZWZ1AxQkY5Q0p6a2RKay9QL05tOWk4cHZiSERtaGRxeTlxTWZnV3Q0cjdwR3RndUtmeUp3QThhMnJaV2dGZjlPUUtjcGRidDhiajRxK2g0RUZTMWNZUDBaeGNCcUVxSDJ1QnZVRlRkWk9tUExJNWN3TDN5T1BQcmhVVGsycStVTzJRaUwvSkk2TnNVZldTOGU3Tm5wQ3RUMy9nazFqbzdrUWtvYzRwQWRpV3dnTVB3YzhodFV2U0FRR3VKdllMY01NZmdOdGtmOEJ4UT09LS1nTXBrYldhQ0pEeWJ3ak9qQjcrTGV3PT0=--1439f36a7f9362aee4b5b666747a2d63d72e81bd'
}
response = requests.request("POST", url, headers=headers, data=payload)
stock_info = response.json()['stock_infos']
stock_data(stock_info)
New Non-Working Code
import requests
import pandas as pd
url = "https://www.stockrover.com/stock_infos/grid?_dc=1644769629231"
def stock_data(stock_info):
data = pd.DataFrame(stock_info)
data.to_csv("data.csv", index=False)
payload = "state={"sortInfo":{},"columns":[77,32,498,500,31,27,499,30,578,28,29,544,181,185,186],"view":281,"priorPrimaryColumn":170,"filterData":[],"name":"New 1","cType":"Screener","cNode":"s_39","cIsFolder":false,"gridSelection":"BTU","lastActive":1396898415,"primaryColumn":76,"folderDisabledParams":{"filterData":[]},"mainGridDateRange":"ytd","groupState":null,"moversGridDateRange":"1_day","peersGridDateRange":"1_day","lastGridSelections":["BTU"],"lastQuantNode":[],"includeQuotesInTable":false,"includeAllQuotesLastValue":false,"markets":{"panel":"summary"},"researchPanel":"tablePanel","recentSearchTickers":["SPY","AMZN","AAPL","s_32","^NDX","AXP","XOM","AFL","^DJX","AIT","ADVC"],"quotesBoxTickers":["AMZN","AAPL","SPY"],"checkedQuotesBoxTickers":["AMZN","AAPL","SPY"],"dashboard":{"buttonRef":"272"},"tickerSelectedFeeds":["Benzinga News","Yahoo News"],"marketSelectedFeeds":["Google News","Stock Market News - Investing.com"],"bondsSelectedFeeds":["Bonds Strategy - Investing.com"],"commoditiesSelectedFeeds":["Commodities & Futures News - Investing.com","Commodities Fundamental Analysis - Investing.com","Commodities Strategy Analysis - Investing.com"],"stocksSelectedFeeds":["CNNMoney News","Google News","Seeking Alpha Top Stories"],"etfsSelectedFeeds":["Economy News - Investing.com","ETF Analysis - Investing.com","Investing Ideas - Investing.com"],"topPanel":"researchPanel","maxRecordsNode":false,"version":7,"lastGridSelectionsRaw":["BTU"],"lastSelectionScreeners":"s_39","quotesDisabled":true,"lastSelectionPortfolios":"p_2","comparisonPanels":{"Portfolio":"p_2","Index":"^DJX","Watchlist":"Watchlists","Screener":"s_39"},"lastSelectionWatchlists":"w_26","indicesSelectedFeeds":["Google News","Yahoo News"],"newsActive":"tickerNews","recentSearchMetrics":["Price","EPS","Sales"],"editPanel":"positionsPanel","newsType":"marketNews","tableColumns":["ticker","rank","score_rank","filter_score","company","cash","currentassets","netppe","intangibles","totalassets","currentliabilities","longtermdebt","totaldebt","totalliabilities","equity","tangiblebookvalue","cash_short_term_p","net_ppe_p","intangibles_p"],"last_save":1644769379,"panels":{"collapsed":{"chp":true,"ip":true,"mp":false,"qp":false,"conp":true,"fsp":false},"viewportWidth":"1920","viewportHeight":"1069","chartPanelHeight":483,"controlPanelWidth":296,"insightPanelWidth":"485","quoteBoxHeight":200,"navigationPanelWidth":277}}&updateMarket=true&page=1&start=0&limit=250"
headers = {
'authority': 'www.stockrover.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
'x-csrf-token': '7yR4pfI0kAArtjJak535 NJrpB0L212PAbXCg0kbyE4SyjFaQ73sMHJLiqAkPb5nGzfC8KvAa3kTADLAEQXyOQ==',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept': 'application/json',
'x-requested-with': 'XMLHttpRequest',
'sec-ch-ua-platform': '"Windows"',
'origin': 'https://www.stockrover.com',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.stockrover.com/research/table/281/s_39/BTU',
'accept-language': 'en-US,en;q=0.9',
'cookie': 'remember_me_pref=0; user_name=test11964; plan=3; premiumBraintreeKey=MIIBCgKCAQEAzM4LJfrNnBOgRFB1dDJkmqTFCWT2Y+ksOydD8xDH4R033WUzxbffMZb+3dqEyQvOVjLcwFIHByDc4Xwej7enas2E/VRyh7Cvyadn7M5zQeRyLcI9Ys5KCozMwxJPc0x76FlXPwiAo1Qlz3RcLb9wGHBag2R51FuTie+hVDCgzWajqDCREzRhi/qlt3D/xXNo/iwJlpOUr/x1QnkkILxgKlq1dD7KJ767O5ojYKXsO+V2Bfu7sSD3djsOxQJ1/RbaDm2E96EDkWhhOeOpPndQ6IuSl4NmnJg/cq6f8csW8M3Ys+MZPFkdxPC4/fRM1XC9o76PjpVNBIO+yJEELKZedwIDAQAB; lr=1644769628; _Ruby2_session=Q1drcmlhazYvUFZLd0NydnRXUGpoUzArZDlxYWRCcW9sRUx5VDBydWVWRHdGWDZlMnlESURzbldwbFV1L0drbUlKaWt5MXRtaS9iR0ZYZEpPVHQ1N25qRnR1d3FrY0tzQW1qQm9CdTZ3MSs0d2c3MlpuMjRiQWhCOHI1cGNWekZ4cUdJd0ZFcGtpeng3MFlqZjFDUW9RYmpFMU9DeGdGMVZKR1EwMjVhSE9yVHl4VXFtQm9aYVBtNHF5d0pwMjJ1aVlNMUVRUzdnVFZWZ1AxQkY5Q0p6a2RKay9QL05tOWk4cHZiSERtaGRxeTlxTWZnV3Q0cjdwR3RndUtmeUp3QThhMnJaV2dGZjlPUUtjcGRidDhiajRxK2g0RUZTMWNZUDBaeGNCcUVxSDJ1QnZVRlRkWk9tUExJNWN3TDN5T1BQcmhVVGsycStVTzJRaUwvSkk2TnNVZldTOGU3Tm5wQ3RUMy9nazFqbzdrUWtvYzRwQWRpV3dnTVB3YzhodFV2U0FRR3VKdllMY01NZmdOdGtmOEJ4UT09LS1nTXBrYldhQ0pEeWJ3ak9qQjcrTGV3PT0=--1439f36a7f9362aee4b5b666747a2d63d72e81bd'
}
response = requests.request("POST", url, headers=headers, data=payload)
stock_info = response.json()['stock_infos']
stock_data(stock_info)
for info in stock_info:
key = info[0]
for i, sub in enumerate(info[1:], 1):
if sub == key:
print(info[i:])
break
CodePudding user response:
The added block of code is added after the csv file has already been written.
You have other problems as well but that is the most obvious at the moment.
it should be closer to...
...
for info in stock_info:
key = info[0]
for i, sub in enumerate(info[1:], 1):
if sub == key:
print(info[i:])
break
stock_data(stock_info)
Also the added block of code doesn't actually make any changes to the data, it only changes what ends up getting printed. Whatever changes you want made to the file need to be made to stock_info
.
CodePudding user response:
If you don't need to keep those columns just slice them out by adding a line to your original code:
data = pd.DataFrame(stock_info)
data = data.iloc[:, 4:]
In original code:
import requests
import pandas as pd
url = "https://www.stockrover.com/stock_infos/grid?_dc=1644769629231"
def stock_data(stock_info):
data = pd.DataFrame(stock_info)
data = data.iloc[:, 4:]
data.to_csv("data.csv", index=False)
payload = "state={"sortInfo":{},"columns":[77,32,498,500,31,27,499,30,578,28,29,544,181,185,186],"view":281,"priorPrimaryColumn":170,"filterData":[],"name":"New 1","cType":"Screener","cNode":"s_39","cIsFolder":false,"gridSelection":"BTU","lastActive":1396898415,"primaryColumn":76,"folderDisabledParams":{"filterData":[]},"mainGridDateRange":"ytd","groupState":null,"moversGridDateRange":"1_day","peersGridDateRange":"1_day","lastGridSelections":["BTU"],"lastQuantNode":[],"includeQuotesInTable":false,"includeAllQuotesLastValue":false,"markets":{"panel":"summary"},"researchPanel":"tablePanel","recentSearchTickers":["SPY","AMZN","AAPL","s_32","^NDX","AXP","XOM","AFL","^DJX","AIT","ADVC"],"quotesBoxTickers":["AMZN","AAPL","SPY"],"checkedQuotesBoxTickers":["AMZN","AAPL","SPY"],"dashboard":{"buttonRef":"272"},"tickerSelectedFeeds":["Benzinga News","Yahoo News"],"marketSelectedFeeds":["Google News","Stock Market News - Investing.com"],"bondsSelectedFeeds":["Bonds Strategy - Investing.com"],"commoditiesSelectedFeeds":["Commodities & Futures News - Investing.com","Commodities Fundamental Analysis - Investing.com","Commodities Strategy Analysis - Investing.com"],"stocksSelectedFeeds":["CNNMoney News","Google News","Seeking Alpha Top Stories"],"etfsSelectedFeeds":["Economy News - Investing.com","ETF Analysis - Investing.com","Investing Ideas - Investing.com"],"topPanel":"researchPanel","maxRecordsNode":false,"version":7,"lastGridSelectionsRaw":["BTU"],"lastSelectionScreeners":"s_39","quotesDisabled":true,"lastSelectionPortfolios":"p_2","comparisonPanels":{"Portfolio":"p_2","Index":"^DJX","Watchlist":"Watchlists","Screener":"s_39"},"lastSelectionWatchlists":"w_26","indicesSelectedFeeds":["Google News","Yahoo News"],"newsActive":"tickerNews","recentSearchMetrics":["Price","EPS","Sales"],"editPanel":"positionsPanel","newsType":"marketNews","tableColumns":["ticker","rank","score_rank","filter_score","company","cash","currentassets","netppe","intangibles","totalassets","currentliabilities","longtermdebt","totaldebt","totalliabilities","equity","tangiblebookvalue","cash_short_term_p","net_ppe_p","intangibles_p"],"last_save":1644769379,"panels":{"collapsed":{"chp":true,"ip":true,"mp":false,"qp":false,"conp":true,"fsp":false},"viewportWidth":"1920","viewportHeight":"1069","chartPanelHeight":483,"controlPanelWidth":296,"insightPanelWidth":"485","quoteBoxHeight":200,"navigationPanelWidth":277}}&updateMarket=true&page=1&start=0&limit=250"
headers = {
'authority': 'www.stockrover.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
'x-csrf-token': '7yR4pfI0kAArtjJak535 NJrpB0L212PAbXCg0kbyE4SyjFaQ73sMHJLiqAkPb5nGzfC8KvAa3kTADLAEQXyOQ==',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'accept': 'application/json',
'x-requested-with': 'XMLHttpRequest',
'sec-ch-ua-platform': '"Windows"',
'origin': 'https://www.stockrover.com',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.stockrover.com/research/table/281/s_39/BTU',
'accept-language': 'en-US,en;q=0.9',
'cookie': 'remember_me_pref=0; user_name=test11964; plan=3; premiumBraintreeKey=MIIBCgKCAQEAzM4LJfrNnBOgRFB1dDJkmqTFCWT2Y+ksOydD8xDH4R033WUzxbffMZb+3dqEyQvOVjLcwFIHByDc4Xwej7enas2E/VRyh7Cvyadn7M5zQeRyLcI9Ys5KCozMwxJPc0x76FlXPwiAo1Qlz3RcLb9wGHBag2R51FuTie+hVDCgzWajqDCREzRhi/qlt3D/xXNo/iwJlpOUr/x1QnkkILxgKlq1dD7KJ767O5ojYKXsO+V2Bfu7sSD3djsOxQJ1/RbaDm2E96EDkWhhOeOpPndQ6IuSl4NmnJg/cq6f8csW8M3Ys+MZPFkdxPC4/fRM1XC9o76PjpVNBIO+yJEELKZedwIDAQAB; lr=1644769628; _Ruby2_session=Q1drcmlhazYvUFZLd0NydnRXUGpoUzArZDlxYWRCcW9sRUx5VDBydWVWRHdGWDZlMnlESURzbldwbFV1L0drbUlKaWt5MXRtaS9iR0ZYZEpPVHQ1N25qRnR1d3FrY0tzQW1qQm9CdTZ3MSs0d2c3MlpuMjRiQWhCOHI1cGNWekZ4cUdJd0ZFcGtpeng3MFlqZjFDUW9RYmpFMU9DeGdGMVZKR1EwMjVhSE9yVHl4VXFtQm9aYVBtNHF5d0pwMjJ1aVlNMUVRUzdnVFZWZ1AxQkY5Q0p6a2RKay9QL05tOWk4cHZiSERtaGRxeTlxTWZnV3Q0cjdwR3RndUtmeUp3QThhMnJaV2dGZjlPUUtjcGRidDhiajRxK2g0RUZTMWNZUDBaeGNCcUVxSDJ1QnZVRlRkWk9tUExJNWN3TDN5T1BQcmhVVGsycStVTzJRaUwvSkk2TnNVZldTOGU3Tm5wQ3RUMy9nazFqbzdrUWtvYzRwQWRpV3dnTVB3YzhodFV2U0FRR3VKdllMY01NZmdOdGtmOEJ4UT09LS1nTXBrYldhQ0pEeWJ3ak9qQjcrTGV3PT0=--1439f36a7f9362aee4b5b666747a2d63d72e81bd'
}
response = requests.request("POST", url, headers=headers, data=payload)
stock_info = response.json()['stock_infos']
stock_data(stock_info)