1
나는 아름다운 단어를 사용하고 있으며 웹 페이지에서 두 단어 사이의 모든 텍스트를 추출하려고합니다.두 단어 사이의 파이썬 텍스트 구문 분석
예는 다음 웹 사이트의 텍스트를 상상 :
This is the text of the webpage. It is just a string of a bunch of stuff and maybe some tags in between.
내가 text
로 시작하고 bunch
로 끝 페이지에 모든 것을 끌어합니다.
이 경우 난 단지 싶어 :
text of the webpage. It is just a string of a bunch
그러나, 한 페이지에이의 여러 인스턴스가있을 수있는 기회가있다.
가장 좋은 방법은 무엇입니까?
이 내 현재 설정입니다 : 당신은 그냥 텍스트를 구문 분석하고 있기 때문에
#!/usr/bin/env python
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
mech = Browser()
urls = [
http://ca.news.yahoo.com/forget-phoning-business-app-sends-text-instead-100143774--sector.html
]
for url in urls:
page = mech.open(url)
html = page.read()
soup = BeautifulSoup(html)
text= soup.prettify()
texts = soup.findAll(text=True)
def visible(element):
if element.parent.name in ['style', 'script', '[document]', 'head', 'title']:
# If the parent of your element is any of those ignore it
return False
elif re.match('<!--.*-->', str(element)):
# If the element matches an html tag, ignore it
return False
else:
# Otherwise, return True as these are the elements we need
return True
visible_texts = filter(visible, texts)
# Filter only returns those items in the sequence, texts, that return True.
# We use those to build our final list.
for line in visible_texts:
print line
감사하지만 난 아무 생각이 될 것입니다 태그의 유형이 없습니다 내가 한 일은 모든 텍스트를 파싱하므로 이제는 텍스트 파싱 질문에 가깝습니다. 이를 보여주기 위해 코드를 업데이트했습니다. – user1328021
귀하의 필요에 맞게 편집 됨 – scripts
내 예제에는'text_from_web_page' 변수가 있어야합니까? – user1328021