abcoder
abcoder

Reputation: 49

Get Stoctwits data with beautiful soup

The following code gives me error:

def get_stocktwits_posts(symbol):
    url = f"https://stocktwits.com/symbol/{symbol}"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    post_count = soup.find("span", class_="st_1Zg91_0").text
    return int(post_count.replace(",", ""))

symbol='TSLA'
current_posts = get_stocktwits_posts(symbol)

error: The following code gives me error:

def get_stocktwits_posts(symbol):
    url = f"https://stocktwits.com/symbol/{symbol}"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    post_count = soup.find("span", class_="st_1Zg91_0").text
    return int(post_count.replace(",", ""))

symbol='TSLA'
current_posts = get_stocktwits_posts(symbol)

Error:
55 current_posts = get_stocktwits_posts(symbol)
     56 prev_week_posts = get_stocktwits_posts(symbol)
     58 # Creazione del dizionario con i dati calcolati

Cell In[3], line 11, in get_stocktwits_posts(symbol)
      9 response = requests.get(url)
     10 soup = BeautifulSoup(response.text, "html.parser")
---> 11 post_count = soup.find("span", class_="st_1Zg91_0").text
     12 return int(post_count.replace(",", ""))

AttributeError: 'NoneType' object has no attribute 'text'

Upvotes: 0

Views: 110

Answers (1)

elebur
elebur

Reputation: 582

Most likely this is due to CloudFlare protection.

When you run this code response = requests.get(url) you get CloudFlare page: enter image description here

You can try using Selenium. But still, you'll have to bypass the protection page.

Upvotes: 0

Related Questions