2011-11-27 2 views
-1
desc = re.compile('<ul class="descShort bullet">(.*)</ul>', re.DOTALL) 
findDesc = re.findall(desc, link_source) 

for i in findDesc: 
    print i 


''' 
<ul class="descShort bullet"> 

     Sleek and distinctive, these eye-catching ornaments will be the star of your holiday decor. These unique glass icicle ornaments are individually handcrafted by artisans in India. 

    </ul> 
''' 

필자는 ul 클래스 태그와/ul 사이의 설명을 추출하려고합니다. 나는 REGEX를 사용하여 soltuion을 찾고 있는데, beautifulsoup.python re.compile 아름다운 스프

+0

당신이 여전히 HTML을 구문 분석 정규식을 사용하는 불행하게도하지만 ...하는 HTML 파서를 사용하는 * 적어도 당신이 * 시도 같아요. –

+0

본 사이트를 처음 접했을 때 어떻게해야합니까? 고맙습니다! – phales15

+1

[질문 목록] (http://stackoverflow.com/users/1018129/aaron-phalen?tab=questions)을 방문하십시오. 그 중 하나에 대한 좋은 대답이 있으면 옆에있는 눈금의 실루엣을 클릭하십시오. – egor83

답변

1

우선 HTML/XML을 정규식으로 구문 분석하는 것은 일반적으로 a bad idea으로 간주됩니다. 따라서 BeautifulSoup와 같은 일부 파서를 사용하는 것이 실제로 더 좋은 아이디어입니다.

은 당신이로 수행 할 수 있습니다 원하는 것은 다음과

from BeautifulSoup import BeautifulSoup 

text = """ 
<ul class="descShort bullet">text1</ul> 
<a href="example.com">test</a> 
<ul class="descShort bullet">one more</ul> 
<ul class="other">text2</ul> 
""" 

soup = BeautifulSoup(text) 

# to get the contents of all <ul> tags: 
for tag in soup.findAll('ul'): 
    print tag.contents[0] 

# to get the contents of <ul> tags w/ attribute class="descShort bullet": 
for tag in soup.findAll('ul', {'class': 'descShort bullet'}): 
    print tag.contents[0] 
관련 문제