2016-08-02 32 views
0

xc.com通貨コンバータのWebサイトに基づいて非常に単純なPython通貨コンバータを作成しようとしていますが、 : プリント(result.contents [0])でPython3 - AttributeError: 'NoneType'オブジェクトに 'contents'属性がありません

ライン16、 はAttributeError: 'NoneType' オブジェクトが、私は何かが何を返していないが、どのようにしていることを理解して同様のNoneTypeのAttributeErrorsを研究した後、何の属性 '内容'

を持っていませんそれが何で、どこでどこを見つけることができますか?コード内のprintステートメントを使用してそのコードを見つけることはできますか?ここで

は私のコードです:

import requests 
from bs4 import BeautifulSoup 

amount = input("Enter amount ") 
currency1 = input("Enter currency1 ") 
currency2 = input("Enter currency2 ") 

url = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" + 
amount + "&From=" + currency1 + "&To=" + currency2 

html_code = requests.get(url).text 

soup = BeautifulSoup(html_code, "html.parser") 

result = soup.find('td', {'class', "rightCol"}) 

print(result.contents[0]) 

私は自作を通じてのPython 3.5.2インストールされているMac OS Xエル・キャピタンのV 10.11.6(15G31) にIDLE version3.5.2を使用しています。任意の助けをいただければ幸いです

Python 3.5.2 (default, Aug 2 2016, 08:10:22) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin 
Type "copyright", "credits" or "license()" for more information. 
>>> 
RESTART: /c2.py 
Enter amount 100 
Enter currency1 usd 
Enter currency2 aud 
Traceback (most recent call last): 
    File "/c2.py", line 16, in <module> 
    print(result.contents[0]) 
AttributeError: 'NoneType' object has no attribute 'contents' 
>>> 

感謝:

は、ここに私のIDLE3出力です!

答えて

3

このエラーはかなり簡単です。resultNoneです。

result = soup.find('td', {'class', "rightCol"}) 

あなたのresult名前がそうするために失敗しBeautifulSoup.findの指標であるNoneオブジェクトにバインドされているため、例外がスローされる代わりに
辞書{'class': "rightCol"}

1

findセット{'class', "rightCol"}を渡していますいくつかの理由があります。おそらく:がここにある,があるからです:

result = soup.find('td', {'class', "rightCol"}) 

これに変更されますか?

result = soup.find('td', {'class': "rightCol"}) 
+0

こんにちはnorokとDeepSpace、残念ながらあなたの提案は何らかの理由で動作しませんでした。ここに私がどのように情報を取得したかのスクリーンショットがあります:[link](http://imageshack.com/a/img921/4307/80dems.png)また、私のシステムにインストールされているパッケージとバージョンの詳細があります。 .beautifulsoup4-4.5.0.dist-info bs4 bs4-0.0.1.dist-info。 BeatuifulSoupがそれほど良くないとすれば、サイトから必要な情報を抽出することができる別の方法がありますか? –

+0

'result = soup.find( 'td'、** {'class':" rightCol "})'これ? 'result = soup.find( 'td'、class = 'rightCol')'これは?またはおそらく、あなたが解析したいHTMLを与えられれば、 'result = soup.find_all( 'td'、class = 'rightCol')'? – norok2

+0

こんにちはnorok2。提案していただきありがとうございますが、3人すべてがIDLE3で「無効な構文」エラーを返し、プログラムをまったく実行できません。 –

関連する問題