以某些单词开头的网页搜索

时间:2013-07-06 13:16:35

标签: python regex beautifulsoup

我正在使用BeautifulSoup和python抓取一个网页,并希望抓住以“Text Start:”开头的页面上的每个句子,如下面的代码所示。每个句子也以逗号结尾,后面跟着月日形式的日期(低于它是5-4)这些实例中有很多,所以我想浏览页面并返回每个开头的句子“文字开始:”。

我一直在尝试使用BeautifulSoup包,但一直遇到麻烦。我认为我应该使用正则表达式,所以我一直在尝试,但还没有真正到达任何地方。

<div class="class">
<div class="time">
<span class="date">07/02/13</span>
<span class="sep">|</span>
<span class="duration">02:15</span>
<div class="clear"></div>
</div>
Text Start: This text changes each time, 5-4
</div>

2 个答案:

答案 0 :(得分:2)

使用regular expression匹配specific text contents

import re

soup.find_all(text=re.compile('^\s*Text Start:.*'))

演示:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <div class="class">
... <div class="time">
... <span class="date">07/02/13</span>
... <span class="sep">|</span>
... <span class="duration">02:15</span>
... <div class="clear"></div>
... </div>
... Text Start: This text changes each time, 5-4
... </div>
... ''')
>>> import re
>>> soup.find_all(text=re.compile('^\s*Text Start:.*'))
[u'\nText Start: This text changes each time, 5-4\n']

答案 1 :(得分:1)

您不需要使用BeautifulSoup来完成这么简单的任务。它比直接使用正则表达式慢得多。

re.findall('^\s*Text Start:.*',page)

在抓取网页时,我发现非常精确地了解页面内容非常有用。 Personnaly,我这样做:

from httplib import HTTPConnection

hypr = HTTPConnection(host='stackoverflow.com',
                      timeout = 3)
rekete = ('/questions/17503336/'
          'scraping-webpage-sentences-'
          'beginning-with-certain-word')

hypr.request('GET',rekete)
page = hypr.getresponse().read()


print '\n'.join('%d %r' % (i,line)
                for i,line in enumerate(page.splitlines(True)))

显示是

0 '<!DOCTYPE html>\r\n'
1 '<html>\r\n'
2 '<head>\r\n'
3 '        \r\n'
4 '    <title>python - Scraping webpage sentences beginning with certain word - Stack Overflow</title>\r\n'
5 '    <link rel="shortcut icon" href="https://cdn.sstatic.net/stackoverflow/img/favicon.ico">\r\n'
6 '    <link rel="apple-touch-icon image_src" href="https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png">\r\n'
7 '    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">\r\n'
8 '    \r\n'
9 '    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>\r\n'
10 '    <script type="text/javascript" src="https://cdn.sstatic.net/js/stub.js?v=d2c9bad99c24"></script>\r\n'
11 '    <link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/stackoverflow/all.css?v=2079d4ae31a4">\r\n'
12 '    \r\n'
13 '    <link rel="canonical" href="http://stackoverflow.com/questions/17503336/scraping-webpage-sentences-beginning-with-certain-word">\r\n'
14 '    <link rel="alternate" type="application/atom+xml" title="Feed for question \'Scraping webpage sentences beginning with certain word\'" href="/feeds/question/17503336">\r\n'
15 '    <script type="text/javascript">\r\n'
16 '        \r\n'
17 '        StackExchange.ready(function () {\r\n'
18 '            StackExchange.using("postValidation", function () {\r\n'
19 "                StackExchange.postValidation.initOnBlurAndSubmit($('#post-form'), 2, 'answer');\r\n"
20 '            });\r\n'
21 '\r\n'
22 '            \r\n'
23 "            StackExchange.question.init({showAnswerHelp:true,totalCommentCount:0,shownCommentCount:0,highlightColor:'#F4A83D',backgroundColor:'#FFF',questionId:17503336});\r\n"
24 '\r\n'
25 '            styleCode();\r\n'
26 '\r\n'
27 "                StackExchange.realtime.subscribeToQuestion('1', '17503336');\r\n"
28 '            \r\n'
29 '                    });\r\n'
30 '    </script>\r\n'
31 '\r\n'

etc etc