2011-11-30 207 views
1
<font class="detDesc">Uploaded 10-29&nbsp;18:50, Size 4.36&nbsp;GiB, ULed by <a class="detDesc" href="/user/NLUPPER002/" title="Browse NLUPPER002">NLUPPER002</a></font> 

提取信息出这我需要 上传10-29 18:50  ,面积4.36  吉布和NLUPPER002在两个单独的阵列。我该怎么做?如何使用美丽的汤在Python

编辑:

这是有很多不同的值这些HTML字体标签的html页面的一部分。我需要一个通用的解决方案,如果有的话使用汤。否则,如所暗示的,我会研究正则表达式。

编辑2:

我对此有疑问。如果我们使用“class”作为遍历汤的关键字,那么它不会与python关键字类一起使用并抛出一个错误吗?

+0

正则表达式可以帮助你轻松做到这一点。 –

+1

@JohnRiselvato不,正则表达式几乎从来不是解析XML/HTML的好解决方案 –

+0

我可以将它转储为JSON,但仍然无法解决我的解决方案,因为此页面的HTML编写得不好。或者我想! – Hick

回答

2
soup = BeautifulSoup(your_data) 
uploaded = [] 
link_data = [] 
for f in soup.findAll("font", {"class":"detDesc"}): 
    uploaded.append(f.contents[0]) 
    link_data.append(f.a.contents[0]) 

例如,使用以下数据:

your_data = """ 
<font class="detDesc">Uploaded 10-29&nbsp;18:50, Size 4.36&nbsp;GiB, ULed by <a class="detDesc" href="/user/NLUPPER002/" title="Browse NLUPPER002">NLUPPER002</a></font> 
<div id="meow">test</div> 
<font class="detDesc">Uploaded 10-26&nbsp;19:23, Size 1.16&nbsp;GiB, ULed by <a class="detDesc" href="/user/NLUPPER002/" title="Browse NLUPPER002">NLUPPER003</a></font> 
""" 

运行上面的代码为您提供:

>>> print uploaded 
[u'Uploaded 10-29&nbsp;18:50, Size 4.36&nbsp;GiB, ULed by ', u'Uploaded 10-26&nbsp;19:23, Size 1.16&nbsp;GiB, ULed by '] 
>>> print link_data 
[u'NLUPPER002', u'NLUPPER003'] 

来获取文本的确切形式,正如你所说,您可以后处理的列表或循环自身内部分析数据。例如:

>>> [",".join(x.split(",")[:2]).replace("&nbsp;", " ") for x in uploaded] 
[u'Uploaded 10-29 18:50, Size 4.36 GiB', u'Uploaded 10-26 19:23, Size 1.16 GiB'] 

附:如果你是列表中理解的粉丝,该解决方案可以作为表达一个班轮:

output = [(f.contents[0], f.a.contents[0]) for f in soup.findAll("font", {"class":"detDesc"})] 

这给了你:

>>> output # list of tuples 
[(u'Uploaded 10-29&nbsp;18:50, Size 4.36&nbsp;GiB, ULed by ', u'NLUPPER002'), (u'Uploaded 10-26&nbsp;19:23, Size 1.16&nbsp;GiB, ULed by ', u'NLUPPER003')] 

>>> uploaded, link_data = zip(*output) # split into two separate lists 
>>> uploaded 
(u'Uploaded 10-29&nbsp;18:50, Size 4.36&nbsp;GiB, ULed by ', u'Uploaded 10-26&nbsp;19:23, Size 1.16&nbsp;GiB, ULed by ') 
>>> link_data 
(u'NLUPPER002', u'NLUPPER003') 
+0

伟大的解决方案。谢谢。我喜欢使用lxml @acorn给出的解决方案。干杯! – Hick

1

您需要用来查找您感兴趣的元素的表达式取决于与文档中的其他元素相比,这些元素的唯一性。因此,如果没有元素的背景,就很难提供帮助。

您是否感兴趣的元素是font元素中的唯一元素,并且其类别为detDesc

如果是这样,在这里是使用lxml溶液:

import lxml.html as lh 

html = ''' 
<font class="detDesc">Uploaded 10-29&nbsp;18:50, Size 4.36&nbsp;GiB, ULed by <a class="detDesc" href="/user/NLUPPER002/" title="Browse NLUPPER002">NLUPPER002</a></font> 
''' 

tree = lh.fromstring(html) 

results = [] 

# iterate over all elements in the document that have a class of "detDesc" 
for el in tree.xpath("//font[@class='detDesc']"): 

    # extract text from the font element 
    first = el.text 

    # extract text from the first <a> within the font element 
    second = el.xpath("a")[0].text 

    results.append((first, second)) 

print results 

结果:

[(u'Uploaded 10-29\xa018:50, Size 4.36\xa0GiB, ULed by ', 'NLUPPER002')] 
+0

哇!很有意思。 – Hick

+0

是的。完美解决方案我可以在我使用过beautifulSoup的其他地方使用相同的概念。或者这将是一个坏主意? – Hick

+0

我的意思是,我使用soup.findAll('a',title ='something')来取代与重复的特定标题的所有链接。 – Hick