标签:data-science beautifulsoup web-scraping valueerror python
我不明白为什么它适用于不同的情况,但不适用于这种情况.
基本上,有位先生帮助我改善了HERE,改进了我的代码以刮除天气,效果很好.然后,我尝试执行相同的操作以刮除范围标记中的ETH值< span class =“ text-large2” data-currency-value =“” $196.01< / span>.因此,我在代码中遵循了相同的技术,替换了字段,并希望它能起作用.
代码在这里:
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标签内是否有$符号?而且,这样程序不知道如何处理字符串?
解决方法:
首先,让我们简化您的示例程序:
>>> 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()))
标签:data-science,beautifulsoup,web-scraping,valueerror,python 来源: https://codeday.me/bug/20191025/1929101.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。