gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:06 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com