2014-07-26 3 views
1

다음 코드를 사용하여 html 페이지를 탐색하고 BeautifulSoup를 사용하여 필요한 데이터를 얻으려고합니다. 모든 것이 좋지만 벽을 치고 붙어 있습니다. 끝에BeautifulSoup에서 html을 텍스트로 추출하는 방법은 무엇입니까?

<p>D: string-1.string2 15030 9h7a2m string3.string<br/> 
D: string-1.string2 15030 9h7a2m string3.string<br/> 
D: string-1.string2 15030 9h7a2m string3.string</p> 
<p><span id="more-1203"></span></p> 
<p>D: string-1.string2 15030 9h7a2m string3.string<br/> 
D: string-1.string2 15030 9h7a2m string3.string<br/> 
D: string-1.string2 15030 9h7a2m string3.string<br/> 
D: string-1.string2 15030 9h7a2m string3.string<br/> 
<p>pinging test is positive but no works</p> 
<p>how much time are online?</p> 
<p><input aria-required="true" id="author" name="author" size="22" tabindex="1" type="text" value=""/> 
<label for="author"><small>Name (required)</small></label></p> 
<p><input aria-required="true" id="email" name="email" size="22" tabindex="2" type="text" value=""/> 
<label for="email"><small>Mail (will not be published) (required)</small></label></p> 
<p><input id="url" name="url" size="22" tabindex="3" type="text" value=""/> 
<label for="url"><small>Website</small></label></p> 
<p><textarea cols="100%" id="comment" name="comment" rows="10" tabindex="4"></textarea></p> 
<p><input id="submit" name="submit" tabindex="5" type="submit" value="Submit Comment"/> 
<input id="comment_post_ID" name="comment_post_ID" type="hidden" value="41"/> 
<input id="comment_parent" name="comment_parent" type="hidden" value="0"/> 
</p> 
<p style="display: none;"><input id="akismet_comment_nonce" name="akismet_comment_nonce" type="hidden" value="1709964457"/></p> 
<p style="display: none;"><input id="ak_js" name="ak_js" type="hidden" value="99"/></p> 

나는에 저장해야합니다

D: string-1.string2 15030 9h7a2m string3 

지고 그 결과 메신저는 이것이다 : 내가 달성하기 위해 필요한 것은

이 줄에서이 9h7a2m 값을 추출하는 것입니다 텍스트 파일.

내 코드 : 사전에

import mechanize 
from bs4 import BeautifulSoup 



# mechanize 

mech = mechanize.Browser() 
mech.set_handle_robots(True) 
mech.set_handle_refresh(True) 
mech.addheaders = [('User-agent', 'Firefox')] 
url = ('http://example.com/') 
response = mech.open(url) 
resp = response.read() 

# beautifulsoup 


soup = BeautifulSoup(resp) 
soup.prettify() 



# test code 



for i in soup.find('div',{'id':'content'}).findAll('p'): 

    print i 

감사합니다.

답변

1

당신은 정규 표현식을 사용하여 추출 할 수 있습니다 :

import re 
from bs4 import BeautifulSoup 

data = """your html here""" 

soup = BeautifulSoup(data) 

s = soup.find('p').br.previous_sibling # find "p" element and get the part before the 1st br 
match = re.search('string\-1\.string2 \d+ (\w+) string3\.string', s) 
print match.group(1) 

인쇄 9h7a2m.


UPD (실제 웹 사이트) :

from urllib2 import urlopen 
from bs4 import BeautifulSoup 

data = urlopen('your URL here') 
soup = BeautifulSoup(data) 

entry = soup.find('div', class_="entry") 

for p in entry.find_all('p'): 
    for row in p.find_all(text=True): 
     try: 
      print row.split(' ')[-2] 
     except IndexError: 
      continue 
+0

난이 오류가'인쇄 match.group (1) AttributeError : 'NoneType'개체가 어떤 속성을 '이없는 group'' – Al1nuX

+0

@ Al1nuX 물론 할 수 있습니다. 내가 제공 한 입력 코드를 테스트했습니다. 당신이 가지고있는 실제 입력은 아마 다른 것입니다. – alecxe

+0

하지만 위의 코드에서 얻은 정확한 출력을 붙여 넣습니다. – Al1nuX

관련 문제