2

我需要<p><a href><rel>从以下 html 代码中提取除等之外的完整文本。

<p>Many of the features that made the Samsung Galaxy S4 one of the most anticipated phones in recent history -- such as its 5-inch 1920 x 1080 <a href="http://www.bubblews.com/news/421662-samsung-galaxy-s4-worlds-first-full-hd-super-amoled-display" rel="nofollow" target="_blank">Full HD Super AMOLED display</a>, its powerful processors (<a href="http://www.samsung.com/global/business/semiconductor/minisite/Exynos/blog_Spotlight_on_the_Exynos5Octa.html" rel="nofollow" target="_blank">Samsung Exynos 5 Octa</a> in the international version and <a href="http://www.qualcomm.com/snapdragon/blog/topics/snapdragon 600" rel="nofollow" target="_blank">Qualcomm Snapdragon 600</a> in the U.S. version) and 16GB, 32GB and 64GB storage options -- are now bringing grief to those who rushed to purchase the fourth-generation Galaxy S series smartphone upon its late April release.</p>

我试过下面的代码

from bs4 import BeautifulSoup
from urllib2 import urlopen

BASE_URL = "http://www.chicagoreader.com"

def get_category_links(section_url):
    html = urlopen(section_url).read()
    soup = BeautifulSoup(html, "lxml")
    for div in soup.findall("div", attrs={'class':'field-content'}):
          print div.find("p").content[0]

但是给出以下输出

使三星 Galaxy S4 成为近代史上最受期待的手机之一的许多功能——例如其 5 英寸 1920 x 1080

我无法获得完整的文本,它应该在 href 和 rel 等标签之后给出文本,请建议我如何获得以下输出。

使三星 Galaxy S4 成为近代史上最受期待的手机之一的许多功能 - 例如其 5 英寸 1920 x 1080 全高清 Super AMOLED 显示其强大的处理器。国际版中的三星 Exynos 5 Octa 和 >高通骁龙 600(美国版)和 16GB、32GB 和 64GB 存储选项——现在让那些在 4 月下旬发布第四代 Galaxy S 系列智能手机时急于购买的人感到悲痛。

谢谢..

4

1 回答 1

3

您可以使用.text

>>> from bs4 import BeautifulSoup
>>> html = '<p>Many of the features that made the Samsung Galaxy S4 one of the most anticipated phones in recent history -- such as its 5-inch 1920 x 1080 <a href="http://www.bubblews.com/news/421662-samsung-galaxy-s4-worlds-first-full-hd-super-amoled-display" rel="nofollow" target="_blank">Full HD Super AMOLED display</a>, its powerful processors (<a href="http://www.samsung.com/global/business/semiconductor/minisite/Exynos/blog_Spotlight_on_the_Exynos5Octa.html" rel="nofollow" target="_blank">Samsung Exynos 5 Octa</a> in the international version and <a href="http://www.qualcomm.com/snapdragon/blog/topics/snapdragon 600" rel="nofollow" target="_blank">Qualcomm Snapdragon 600</a> in the U.S. version) and 16GB, 32GB and 64GB storage options -- are now bringing grief to those who rushed to purchase the fourth-generation Galaxy S series smartphone upon its late April release.</p>'
>>> soup = BeautifulSoup(html)
>>> print soup.p.text
Many of the features that made the Samsung Galaxy S4 one of the most anticipated phones in recent history -- such as its 5-inch 1920 x 1080 Full HD Super AMOLED display, its powerful processors (Samsung Exynos 5 Octa in the international version and Qualcomm Snapdragon 600 in the U.S. version) and 16GB, 32GB and 64GB storage options -- are now bringing grief to those who rushed to purchase the fourth-generation Galaxy S series smartphone upon its late April release.
于 2013-05-06T10:49:53.517 回答