简体   繁体   English

两个单词之间的Python文本解析

[英]Python text parsing between two words

I'm using beautifulsoup and want to extract all text from between two words on a webpage.我正在使用 beautifulsoup 并想从网页上的两个单词之间提取所有文本。

Ex, imagine the following website text:例如,想象以下网站文本:

This is the text of the webpage. It is just a string of a bunch of stuff and maybe some tags in between.

I want to pull out everything on the page that starts with text and ends with bunch .我想拉出页面上以text开头并以bunch结尾的所有内容。

In this case I'd want only:在这种情况下,我只想:

text of the webpage. It is just a string of a bunch 

However, there's a chance there could be multiple instances of this on a page.但是,一个页面上可能有多个这样的实例。

What is the best way to do this?做这个的最好方式是什么?

This is my current setup:这是我目前的设置:

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

since you're just parsing the text you just need the regex:由于您只是在解析文本,因此您只需要正则表达式:

import re
result = re.findall("text.*?bunch", text_from_web_page)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM