2017-12-06 4 views
0

私は美容院を使って不動産販売サイトを掻き集めるpythonスクリプトを持っています。 私はHTMLからベッドの数を取得しようとしています。BeautifulSoupデータアジドを見つけよう

検索結果のリストごとにdata-reactidが変更されます。番号11606747はユニークです。 私はこの例ではベッド数= 3を返すために "* $ beds.0.0"のワイルドカード検索を試みています。

エラーメッセージはありません。コードは実行されますが、数値は返されません。

私は間違っていますか?

HTML:

<div class="property-features is-regular listing-result__features" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2"><span class="property-feature__feature" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds"><span class="property-feature__feature-text-container" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0"><span data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0.0">3</span><span data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0.1"> </span><span class="property-features__feature-text" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0.2">Beds</span></span> 

ザ・Pythonコード

beds = listing.findAll('span',{"data-reactid":re.compile('*$beds.0.0')}) 
+0

は、編集されたコードを参照してください。私はすでにそれを更新しました。あなたが望むアウトプットは「3ベッド」だと思います。しかし、あなたの投稿を編集しようとしましたが、依然として必要な出力についての明確さは得られません。次の記事@ Mark Waltersで明示的にしてください。 – SIM

答えて

0

あなたをベッドの状態を得るためにこれを試すことができます:

content=''' 
<html> 
<body> 
    <div class="property-features is-regular listing-result__features" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2"> 
    <span class="property-feature__feature" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds"> 
    <span class="property-feature__feature-text-container" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0"> 
    <span data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0.0"> 
     3 
    </span> 
    <span data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0.1"> 
    </span> 
    <span class="property-features__feature-text" data-reactid=".1e881obdfqe.3.1.3.1:$11606747.0.1.0.2.$beds.0.2"> 
     Beds 
    </span> 
    </span> 
    </span> 
    </div> 
</body> 
</html> 
''' 
from bs4 import BeautifulSoup 
soup = BeautifulSoup(content,"lxml") 
item = soup.select("div span[data-reactid*='$11606747']")[0].text 
print(' '.join(item.split())) 

結果:

3 Beds 
+0

が素晴らしいです。ありがとう! –

0

彼らは正規表現で特別なので、あなたは、シンボル$.*をエスケープする必要があります。

re.compile(r'\*\$beds\.0\.0') 
+0

これはまだ空リストを返しています: プリントベッドは[] –

関連する問題