python - 获取未定义类的段落的内容

标签 python beautifulsoup

我正在尝试使用 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/

相关文章:

python - 如果有GIL,那么python中多线程环境中的锁有什么用呢?

python - Django Rest Framework 中的序列化器字段过滤?

python - 网页抓取 : request not returning complete content of the webpage

python - Beautifulsoup findAll,如何获取第二条文本

python - 使用 Python 抓取时丢失数据?

python - BeautifulSoup 和 urllib 从网站查找数据

python - Django-复数形式的无效 token : EXPRESSION

python - 编译 Python

python - 在 Google Colab 中使用 plotly 绘制超过 600 万行的数据集

python - BeautifulSoup:如何以datwtime格式发布YouTube视频的日期时间?