繁体   English   中英

使用Beautiful Soup和Python解析元标记

[英]parsing meta tag with Beautiful Soup and Python

我使用Beautiful Soup 3和python 2.6解析HTML页面时遇到问题。

HTML内容是这样的:

content='<div class="egV2_EventReportCardLeftBlockShortWidth">
<span class="egV2_EventReportCardTitle">When</span>
<span class="egV2_EventReportCardBody">
<meta itemprop="startDate" content="2012-11-23T10:00:00.0000000">
<span class='egV2_archivedDateEnded'>STARTS</span>Fri 23 Nov,10:00AM<br/>
<meta itemprop="endDate" content="2012-12-03T18:00:00.0000000">
<span class='egV2_archivedDateEnded'>ENDS</span>Mon 03 Dec,6:00PM</span>
<span class="egV2_EventReportCardBody"></span>
<div class="egV2_div_cal" onclick=" showExportEvent()">
<div class="egV2_div_cal_outerFix">
<div class="egV2_div_cal_InnerAdjust"> Cal </div>
</div></div></div>'

我想把字符串'11月23日星期五,10:00AM'从中间变成一个变量,用于连接,然后发送回PHP页面。

要阅读此内容,我使用以下代码:(以上内容来自html页面阅读(http://everguide.com.au/melbourne/event/2012-nov-23/life-with-bird-spring - 仓储 - 销售/)

import urllib2
req = urllib2.Request(URL)
response = urllib2.urlopen(req)
html = response.read()
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html.decode('utf-8'))
soup.prettify()
import re
for node in soup.findAll(itemprop="name"):
    n = ''.join(node.findAll(text=True)) 
for node in soup.findAll("div", { "class" : "egV2_EventReportCardLeftBlockShortWidth" }):
    d = ''.join(node.findAll(text=True))
print n,"|", d

哪个回报:

[(ssh user)]# python testscrape.py

LIFE with BIRD Spring Warehouse Sale | 
When
<span class="egV2_EventReportCardDateTitle">STARTS</span>
STARTSFri 23 Nov,10:00AMENDSMon 03 Dec,6:00PM
<span class="egV2_EventReportCardDateTitle">ENDS</span>



 Cal 



[(ssh user)]# 

(它包括所有这些换行等)。

因此,您可以在最后看到,我将这两个被剥离的字符串分组到一个打印输出中,在PHP的中间有一个分隔符,可以将字符串读回一个,然后将其拆分。

问题是 - python代码可以读取该页面并存储文本,但它包含所有那些混乱PHP应用程序的垃圾和标签等。

我真的只想回来:

Fri 23 Nov,10:00AM

是因为我使用findAll(text = True)方法?

如何向下钻取并仅获取该div中的文本 - 而不是span标签?

非常感谢任何帮助,谢谢。

里克 - 墨尔本。

为什么不尝试类似的东西

In [95]: soup = BeautifulSoup(content)

In [96]: soup.find("span", {"class": "egV2_archivedDateEnded"})
Out[96]: <span class="egV2_archivedDateEnded">STARTS</span>

In [97]: soup.find("span", {"class": "egV2_archivedDateEnded"}).next
Out[97]: u'STARTS'

In [98]: soup.find("span", {"class": "egV2_archivedDateEnded"}).next.next
Out[98]: u'Fri 23 Nov,10:00AM'

甚至

In [99]: soup.find("span", {"class": "egV2_archivedDateEnded"}).nextSibling
Out[99]: u'Fri 23 Nov,10:00AM'

如果您只是尝试提取一个易于使用特定属性标识的单个标记,则pyparsing会使这非常简单(我会使用其ISO8601时间字符串值继续使用元标记):

from pyparsing import makeHTMLTags,withAttribute

meta = makeHTMLTags('meta')[0]
# only want matching <meta> tags if they have the attribute itemprop="startDate"
meta.setParseAction(withAttribute(itemprop="startDate"))

# scanString is a generator that yields (tokens,startloc,endloc) triples, we just 
# want the tokens
firstmatch = next(meta.scanString(content))[0]

现在转换为datetime对象,可以按照您喜欢的方式进行格式化,写入数据库,用于计算经过时间等:

from datetime import datetime
dt = datetime.strptime(firstmatch.content[:19], "%Y-%m-%dT%H:%M:%S")

print (firstmatch.content)
print (dt)

打印:

2012-11-23T10:00:00.0000000
2012-11-23 10:00:00

暂无
暂无

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

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