我正在尝试使用 Beautiful Soup 从充满文本的段落中提取内容,但我的所有内容都标记有 <P>
标签。我想要获取的段落内容没有定义类。
这是我要从中提取数据的 URL:http://www.washingtonpost.com/world/middle_east/turkeys-erdogan-to-be-sworn-in-as-president/2014/08/28/7461617c-2e7e-11e4-be9e-60cc44c01e7f_story.html .
我尝试过的代码是:
import urllib2
from bs4 import BeautifulSoup
target_url = "http://www.washingtonpost.com/world/middle_east/turkeys-erdogan-to-be- sworn-in-as-president/2014/08/28/7461617c-2e7e-11e4-be9e-60cc44c01e7f_story.html"
data = urllib2.urlopen(target_url).read()
soup = BeautifulSoup(data)
paragraphs = soup.find_all("p")
for p in paragraphs:
print p
最佳答案
网站使用正确的 HTML5 标签,使用
article = soup.find('article')
content = article.get_text()
关于python - 获取未定义类的段落的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25748306/