2013-05-06 92 views
2

我需要從以下html代碼中提取除<p><a href><rel>等之外的完整文本。如何使用beautifulsoup提取段標記中的完整文本

<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在國際上「美國版高通Snapdragon 600)以及16GB,32GB和64GB存儲選件 - 現在正在爲那些在4月底發佈的第四代Galaxy S系列智能手機購買產品而感到悲痛。

謝謝..

回答

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. 
+0

謝謝,它的工作,但我需要從網站中提取完整的文本,我不應該在html變量中進行硬編碼值,如你在上面提到的代碼中提到的,它應該從url中提取,正如我在代碼中提到的,請建議我這個怎麼做。 – 2013-05-06 11:14:14

+0

@vittalcherala對不起,我想但我似乎無法讓你的代碼工作。也許網站改變了? – TerryA 2013-05-06 11:25:42

相關問題