我不明白为什么它适用于不同的场景,但不适用于此场景。
基本上,一些绅士帮助我HERE改进我的代码以刮除天气,这完美地运作。然后我尝试做同样的事情来刮取跨度标记<span class="text-large2" data-currency-value="">$196.01</span>
中的ETH值。所以,我在代码中遵循相同的技术,替换了字段,并希望它能够工作。
代码在这里:
import requests
from BeautifulSoup import BeautifulSoup
import time
url = 'https://coinmarketcap.com/currencies/litecoin/'
def ltc():
while (True):
response = requests.get(url)
soup = BeautifulSoup(response.content)
price_now = int(soup.find("div", {"class": "col-xs-6 col-sm-8 col-md-4 text-left"}).find(
"span", {"class": "text-large2"}).getText())
print(u"LTC price is: {}{}".format(price_now))
# if less than 150
if 150 > price_now:
print('Price is Low')
# if more than 200
elif 200 < price_now:
print('Price is high')
if __name__ == "__main__":
ltc()
输出如下:
Traceback (most recent call last):
File "test2.py", line 24, in <module>
ltc()
File "test2.py", line 13, in ltc
"span", {"class": "text-large2"}).getText())
ValueError: invalid literal for int() with base 10: '196.01'
然后,我终于以这种方式尝试了;但从这里我得到误报,但没有错误。它打印任何想要的东西
import requests
from bs4 import BeautifulSoup
import time
url = 'https://coinmarketcap.com/currencies/litecoin/'
def liteCoin():
while (True):
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
value = soup.find('span', {'class': 'text-large2'})
print(''.join(value.stripped_strings))
if 150 > value: # if less than 150
print('Price is Low!')
elif 200 < value: # if more than 200
print('Price is High')
else:
print('N/A')
time.sleep(5)
if __name__ == "__main__":
liteCoin()
问题是ETH的值在$
内有span tag
符号吗?而且,这样程序不知道如何处理字符串?
答案 0 :(得分:2)
首先,让我们简化您的示例程序:
>>> int('196.01')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '196.01'
无法将字符串'196.01'
转换为整数。
试试这个:
>>> int(float('196.01'))
196
从简单回到复杂,我们可以这样做:
#UNTESTED
price_now = int(float(soup.find("div", {"class": "col-xs-6 col-sm-8 col-md-4 text-left"}).find(
"span", {"class": "text-large2"}).getText()))
答案 1 :(得分:2)
你需要理解Python中的类型,你得到的浮点数不是int,你需要将float转换为字符串来打印它。所以需要进行两项更改。
price_now = float(soup.find("div", {"class": "col-xs-6 col-sm-8 col-md-4 text-left"}).find("span", {"class": "text-large2"}).getText())
print(u"LTC price is: {}".format(str(price_now)))
输出:
LTC price is: 195.44
LTC price is: 195.44