2016-08-21 2 views
3

私はウェブスクレイピングを初めて利用しています。私はPythonを使用してデータをスクラップしています。 誰かからデータを抽出する方法で私を助けることができる:divタグに含まれる強い要素を抽出するには

<div class="dept"><strong>LENGTH:</strong> 15 credits</div> 

私の出力は、長さでなければなりません。ここで15 credits

は私のコードです:

from urllib.request import urlopen 
from bs4 import BeautifulSoup 

length=bsObj.findAll("strong") 
for leng in length: 
    print(leng.text,leng.next_sibling) 

出力:

DELIVERY: Campus 
LENGTH: 2 years 
OFFERED BY: Olin Business School 

しかし、私は長さだけを持っていたいと思います。

ウェブサイト:http://www.mastersindatascience.org/specialties/business-analytics/

答えて

3

あなたはテキストによってstrong要素を見つけるためにあなたのコードビットを向上させる必要があります。

soup.find("strong", text="LENGTH:").next_sibling 

あるいは、複数の長さのために:

for length in soup.find_all("strong", text="LENGTH:"): 
    print(length.next_sibling.strip()) 

デモ:

>>> import requests 
>>> from bs4 import BeautifulSoup 
>>> 
>>> url = "http://www.mastersindatascience.org/specialties/business-analytics/" 
>>> response = requests.get(url) 
>>> soup = BeautifulSoup(response.content, "html.parser") 
>>> for length in soup.find_all("strong", text="LENGTH:"): 
...  print(length.next_sibling.strip()) 
... 
33 credit hours 
15 months 
48 Credits 
... 
12 months 
1 year 
関連する問題