2016-03-31 10 views
0

私はbeautifulsoupでhtmlデータをスクラップしようとしています。クラスを所有しているクラスをどのようにスクラップしているのですか? (ダブルクラス)。ここに私のhtmlコードを理解するために:beautifulsoupを使った複数のクラス

<span class="phone-contacts"> 
 
    <span class = "phone"> 
 
    <span class = "label"> 
 
     phone 
 
    </span> 
 
    <span class = "value"> 
 
     <a class="tel" href="tel:+41XXXXXX"> 
 
     021 XXX XX XX 
 
     </a> 
 
    </span> 
 
    </span> 
 
    <span class = "mobile"> 
 
    <span class = "label"> 
 
    Mobile 
 
    </span> 
 
    <span class = "value"> 
 
     <a class="tel" href="tel:+41XXXXXX"> 
 
     079 XXX XX XX 
 
     </a> 
 
    </span> 
 
    </span> 
 
</span>

あなたは携帯電話やモバイルを定義する最後のクラスは、「TEL」であり、それは私の問題だ、私は携帯電話を利用したいことがわかりますこのようなseparetlyのような辞書:

def bot_get_data(item_url): 
    source_code = requests.get(item_url) 
    plain_text = source_code.text 
    soup = BeautifulSoup(plain_text, "html.parser") 
    name_company = soup.find_all("h1") 
    phone_number = soup.find_all("a", {"class": "phone"}, {"class": "tel"}) 
    #my problem is here : I have to find a way to go in a class who owns another 
    mobile_number = soup.find_all("a",{"class": "mobile"}, {"class": "tel"}) 
    site_name = soup.find_all("a", {"class": "redirect"}) 
    email_name = soup.find_all("a", href=re.compile('mailto')) 

    name_data = [] 
    phone_data = [] 
    mobile_data = [] 
    site_data = [] 
    mail_data = [] 

    for item in name_company: 
     name_data.append(item.string) 
     print(item.string) 

    for num in phone_number: 
     phone_data.append(num.string) 
     print(num.string) 

    for mob in mobile_number: 
     mobile_data.append(mob.string) 
     print(mob.string) 

    for site in site_name: 
     site_data.append(site.string) 
     print(site.string) 


    for email in email_name: 
     mail_data.append(email.string) 
     print(email.string) 

beautifulsoupでそれを行う方法を知っている人はいますか?

感謝=)

答えて

0

使用BeautifulSoup.select()

tel = soup.select(".phone .value .tel")[0].text.strip() 
mob = soup.select(".mobile .value. .tel")[0].text.strip() 
関連する問題