2016-12-13 3 views
-2

일반적인 td/tr 형식이 아니고 이미지가 포함 된 표를 html에서 다운로드하려고합니다.Python을 사용하여 이미지를 포함한 html의 표 추출

HTML 코드는 다음과 같습니다 : [오십 포인트의 서비스 (45) 오십 점, 에서 위치 45] :

<div class="dynamicBottom"> 
<div class="dynamicLeft"> 
<div class="content_block details_block scroll_tabs" data-tab="TABS_DETAILS"> 
<div class="header_with_improve wrap"> 
<a href="/UpdateListing.html" onclick="ta.setEvtCookie('UpdateListing', 'entry-detail-moreinfo', null, 0, '/UpdateListingRedesign')"><div class="improve_listing_btn ui_button primary small">improve this entry</div></a> 
<h3 class="tabs_header">Details</h3> </div> 
<div class="details_tab"> 
<div class="table_section"> 
<div class="row"> 
<div class="ratingSummary wrap"> 
<div class="histogramCommon bubbleHistogram wrap"> 
<div class="colTitle"> 
Rating 
</div> 
<ul class="barChart"> 
<li> 
<div class="ratingRow wrap"> 
<div class="label part "> 
<span class="text">Location</span> 
</div> 
<div class="wrap row part "> 
<span class="rate sprite-rating_s rating_s"> <img class="sprite-rating_s_fill rating_s_fill s45" src="https://static.tacdn.com/img2/x.gif" alt="45 out of fifty points"> 
</span> 
</div> 
</div> 
<div class="ratingRow wrap"> 
<div class="label part "> 
<span class="text">Service</span> 
</div> 
<div class="wrap row part "> 
<span class="rate sprite-rating_s rating_s"> <img class="sprite-rating_s_fill rating_s_fill s45" src="https://static.tacdn.com/img2/x.gif" alt="45 out of fifty points"> 
</span> 
</div> 
</div> 
</li> 

나는 테이블을 좀하고 싶습니다.

다음 코드는 "위치"와 "서비스"만 인쇄하고 등급은 포함하지 않습니다.

for url in urls: 
    r=requests.get(url) 
    time.sleep(delayTime) 
    soup=BeautifulSoup(r.content, "lxml") 
    data17= soup.findAll('div', {'class' :'dynamicBottom'}) 
    for item in (data17): 
     print(item.text) 

그리고 코드

data18= soup.find(attrs={'class': 'sprite-rating_s_fill rating_s_fill s45'}) 
print(data18["alt"] if data18 else "No meta title given") 

가 명확하지 않기 때문에 어느 쪽이든 그것은 단지 "45 쉰 점에서"출력합니다하지만 어떤 범주에 대한 명확하지 않기 때문에이 나타내는 평가 도움이되지 않습니다 . 또한 이미지 태그 ('sprite-rating_s_fill rating_s_fill s45')는 등급에 따라 다른 표에서 다릅니다.

전체 테이블을 추출 할 수있는 방법이 있습니까? 또는 특정 단어 뒤에 이미지를 추출하도록 Python에 지시하는 것. "위치"?

도움 주셔서 감사합니다.

답변

0
html = '''<div class="dynamicBottom"> 
<div class="dynamicLeft"> 
<div class="content_block details_block scroll_tabs" data-tab="TABS_DETAILS"> 
<div class="header_with_improve wrap"> 
<a href="/UpdateListing.html" onclick="ta.setEvtCookie('UpdateListing', 'entry-detail-moreinfo', null, 0, '/UpdateListingRedesign')"><div class="improve_listing_btn ui_button primary small">improve this entry</div></a> 
<h3 class="tabs_header">Details</h3> </div> 
<div class="details_tab"> 
<div class="table_section"> 
<div class="row"> 
<div class="ratingSummary wrap"> 
<div class="histogramCommon bubbleHistogram wrap"> 
<div class="colTitle"> 
Rating 
</div> 
<ul class="barChart"> 
<li> 
<div class="ratingRow wrap"> 
<div class="label part "> 
<span class="text">Location</span> 
</div> 
<div class="wrap row part "> 
<span class="rate sprite-rating_s rating_s"> <img class="sprite-rating_s_fill rating_s_fill s45" src="https://static.tacdn.com/img2/x.gif" alt="45 out of fifty points"> 
</span> 
</div> 
</div> 
<div class="ratingRow wrap"> 
<div class="label part "> 
<span class="text">Service</span> 
</div> 
<div class="wrap row part "> 
<span class="rate sprite-rating_s rating_s"> <img class="sprite-rating_s_fill rating_s_fill s45" src="https://static.tacdn.com/img2/x.gif" alt="45 out of fifty points"> 
</span> 
</div> 
</div> 
</li>''' 
from bs4 import BeautifulSoup 
soup = BeautifulSoup(html, 'lxml') 
for div in soup.find_all('div', class_="ratingRow wrap"): 
    text = div.text.strip() 
    alt = div.find('img').get('alt') 
    print(text, alt) 

아웃 :

Location 45 out of fifty points 
Service 45 out of fifty points 
+0

@F. Paul은이 질문을 끝내기 위해 동의하십시오. –

관련 문제