import requests
from bs4 import BeautifulSoup as BS
import tkinter as tk
class Scraping:
@classmethod
def get_to_site(cls, stock_name):
sitename = 'https://www.nasdaq.com/market-activity/stocks/' + stock_name
site = requests.get(f'{sitename}')
print(site.status_code)
def get_price():
pass
class GUI:
pass
Scraping.get_to_site('tsla')
So I'm doing an app that should take stock prices from a site and show them graphically. I had barely started when a problem occured. I tried to print the status code of my requests site to check if it finds the right site. When I run this, nothing appears to the console. Console looks like this:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\Aatu\Documents\python\pythonleikit> & C:/Python39ni/python.exe c:/Users/Aatu/Documents/python/pythonleikit/stock_price_scraper.py
I've waited for a long time to have something appearing in the console but nothing happened. Why?
Adding these headers to your request will do the trick.
class Scraping:
@classmethod
def get_to_site(cls, stock_name):
sitename = 'https://www.nasdaq.com/market-activity/stocks/' + stock_name
site = requests.get(sitename, headers={
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"en-GB,en;q=0.9,en-US;q=0.8,ml;q=0.7",
"Connection":"keep-alive",
"Host":"www.nasdaq.com",
"Referer":"http://www.nasdaq.com",
"Upgrade-Insecure-Requests":"1",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"
})
print(site.status_code)
def get_price():
pass
class GUI:
pass
Scraping.get_to_site('TSLA')