Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Striping the empty line
#1
Hi guys i am having this weird issue where this empty space appears even so after calling the strip() build in python program.
Here is the code i have been working on lately

#create a simple script that will visit the website and check out the btc price in real time

from bs4 import BeautifulSoup
import requests

requestbtc = requests.get("https://bitscreener.com/coins/bitcoin")
requestseth = requests.get("https://bitscreener.com/coins/ethereum")
content1 = requestbtc.content
content2 = requestseth.content
soup1 = BeautifulSoup(content1,"html.parser")
soup2 = BeautifulSoup(content2,"html.parser")

element1 = soup1.find('div',{"class":"header-price-container"})
btc = element1.text.strip()

element2 = soup2.find('div',{"class":"header-price-container"})
eth = element2.text.strip()

print("-------------BTC Price------------")
print(btc)
print("\n")
print("-------------ETH Price------------")
print(eth)
print("\n")


#The Html elements are same for both
# <div class="header-price-container">
# 	<div class="header-price-main">
# 		<span style="background: inherit; padding: 0px 1px;">
# 			<span style="color: inherit;">$206.5</span>
# 			<span style="color: inherit;">4</span>
# 		</span>
# 	</div>
# <div class="header-price-sub">
# 	<span style="color: rgb(0, 153, 51);">+$4.3349&nbsp;</span>
# 	<span style="color: rgb(0, 153, 51);">(+2.14%)</span>
# 	</div>
# </div>

Output

>python btc.py 
-------------BTC Price------------
$9,182.77


+$21.09 
(+0.23%)


-------------ETH Price------------
$207.36


+$4.2616 
(+2.10%)



The empty space appears on the price example
$9,182.77


+$21.09
(+0.23%)

What i think is that i am not calling the right class element or maybe i simply call and not align it what might be the wrong thing i am doing in this code.

Ok i think i might have solved the issue by replacing it

element1 = soup1.find('div',{"class":"header-price-container"})
btc = element1.text.strip()
btc = btc.replace('\n',' \t ')

element2 = soup2.find('div',{"class":"header-price-container"})
eth = element2.text.strip()
eth = eth.replace('\n',' \t ')
One thing left is that can i keep this program running on the terminal and see the price change's in real time using the terminal how can i accomplish this in python
Quote
#2
(May-23-2020, 02:49 PM)Calli Wrote: One thing left is that can i keep this program running on the terminal and see the price change's in real time using the terminal how can i accomplish this in python
Output:
E:\div_code\scrape λ python stock_schedule.py --- Sat, May 23, 2020 10:56 PM --- -------------BTC Price------------ $9,199.46 +$5.0197  (+0.05%) -------------ETH Price------------ $207.42 -$0.5514  (-0.27%) --- Sat, May 23, 2020 10:57 PM --- -------------BTC Price------------ $9,199.46 +$5.0197  (+0.05%) -------------ETH Price------------ $207.42 -$0.5514  (-0.27%) --- Sat, May 23, 2020 10:58 PM --- -------------BTC Price------------ $9,199.46 +$5.0197  (+0.05%) -------------ETH Price------------ $207.42 -$0.5514  (-0.27%) --- Sat, May 23, 2020 10:59 PM --- -------------BTC Price------------ $9,199.46 +$5.0197  (+0.05%) -------------ETH Price------------ $207.42 -$0.5514  (-0.27%)
Something like this,so here using schedule.
So need to organize your code in functions,as i already has written this then can just show code rather than bother with hints.
# pip install schedule, pendulum  
from bs4 import BeautifulSoup
import requests
import schedule
import pendulum
import time

def stock():
    requestbtc = requests.get("https://bitscreener.com/coins/bitcoin")
    requestseth = requests.get("https://bitscreener.com/coins/ethereum")
    content1 = requestbtc.content
    content2 = requestseth.content
    soup1 = BeautifulSoup(content1,"html.parser")
    soup2 = BeautifulSoup(content2,"html.parser")

    element1 = soup1.find('div',{"class":"header-price-container"})
    btc = element1.text.strip().replace('\n',' \t ')
    element2 = soup2.find('div',{"class":"header-price-container"})
    eth = element2.text.strip().replace('\n',' \t ')
    return btc, eth

def display_prices(btc, eth):
    now = pendulum.now()
    print(f'--- {now.to_day_datetime_string()} ---' )
    print("-------------BTC Price------------")
    print(btc)
    print("-------------ETH Price------------")
    print(eth)
    print('\n' * 2)

if __name__ == '__main__':
    btc, eth =  stock()
    #display_prices(btc, eth)
    schedule.every(1).minutes.do(display_prices, btc=btc, eth=eth)

    while 1:
        schedule.run_pending()
        time.sleep(1)
Calli likes this post
Quote
#3
>python stock.py
Traceback (most recent call last):
  File "stock.py", line 34, in <module>
    btc, eth = stock()
TypeError: cannot unpack non-iterable NoneType object
I am getting TypeError
Quote
#4
Check network try run it again.
It's same as you first code just that i have organized in function for running schedule and added show date/time.
Try running your original code again and see if that work.
Quote
#5
Yes my original code runs just fine but the one you modified doesn't work i tried multiple times and i am getting only this error

>python stock.py
Traceback (most recent call last):
  File "stock.py", line 34, in <module>
    btc, eth = stock()
TypeError: cannot unpack non-iterable NoneType object
Quote
#6
What version of python are you running?
If run stock() function alone do it return the result.
from bs4 import BeautifulSoup
import requests
import time

def stock():
    requestbtc = requests.get("https://bitscreener.com/coins/bitcoin")
    requestseth = requests.get("https://bitscreener.com/coins/ethereum")
    content1 = requestbtc.content
    content2 = requestseth.content
    soup1 = BeautifulSoup(content1,"html.parser")
    soup2 = BeautifulSoup(content2,"html.parser")

    element1 = soup1.find('div',{"class":"header-price-container"})
    btc = element1.text.strip().replace('\n',' \t ')
    element2 = soup2.find('div',{"class":"header-price-container"})
    eth = element2.text.strip().replace('\n',' \t ')
    return btc, eth

print(stock())
Output:
('$9,071.34 \t \t \t -$106.24\xa0 \t (-1.16%)', '$205.60 \t \t \t -$1.8007\xa0 \t (-0.87%)')
The error indicate that it return None,also same what BS return if do not find the tag.
Just to make the same error.
def stock():
    return None

>>> btc, eth = stock()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: cannot unpack non-iterable NoneType object
Quote
#7
I am using Python 3.7.3

# pip install schedule, pendulum

from bs4 import BeautifulSoup
import requests
import schedule
import pendulum
import time


def stock():
    requestbtc = requests.get("https://bitscreener.com/coins/bitcoin")
    requesteth = requests.get("https://bitscreener.com/coins/ethereum")
    content1 = requestbtc.content
    content2 = requesteth.content
    soup1 = BeautifulSoup(content1, "html.parser")
    soup2 = BeautifulSoup(content2, "html.parser")

    element1 = soup1.find('div', {"class": "header-price-container"})
    btc = element1.text.strip().replace('\n', ' \t ')
    element2 = soup2.find('div', {"class": "header-price-container"})
    eth = element2.text.strip().replace('\n', ' \t ')
    return btc, eth


def display_prices(btc, eth):
    now = pendulum.now()
    print(f'--- {now.to_day_datetime_string()} ---')
    print("-------------BTC Price------------")
    print(btc)
    print("-------------ETH Price------------")
    print(eth)
    print('\n' * 2)

if __name__ == '__main__':
    btc, eth = stock()
    # display_prices(btc, eth)
    schedule.every(1).minutes.do(display_prices, btc=btc, eth=eth)

    while 1:
        schedule.run_pending()
        time.sleep(1)
Thanks a lot after returning btc, and eth it worked fine
Quote
#8
That's not a copy of all of my code,you should see that line under is missing in code you posted.
return btc, eth
Calli likes this post
Quote
#9
Thanks a lot after returning it's working like i expected
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)