1

私は美しいスープを使用しており、Web ページの 2 つの単語の間からすべてのテキストを抽出したいと考えています。

例として、次の Web サイトのテキストを想像してください。

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 

ただし、1 つのページにこれの複数のインスタンスが存在する可能性があります。

これを行う最善の方法は何ですか?

これは私の現在の設定です:

#!/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
4

1 に答える 1

2

テキストを解析しているだけなので、正規表現が必要です。

import re
result = re.findall("text.*?bunch", text_from_web_page)
于 2012-11-22T03:44:17.900 に答える