美丽汤文章抓取

3

我正在尝试获取文章正文中所有的 p 标签。请问有人能解释一下我的代码有什么问题,以及如何改进它?以下是文章的 URL 和相关代码。感谢您提供的任何见解。

URL: http://www.france24.com/en/20140310-libya-seize-north-korea-crude-oil-tanker-rebels-port-rebels/

import urllib2
from bs4 import BeautifulSoup

# Ask user to enter URL
url = raw_input("Please enter a valid URL: ")

soup = BeautifulSoup(urllib2.urlopen(url).read())

# retrieve all of the paragraph tags
body = soup.find("div", {'class':'bd'}).get_text()
for tag in body:
    p = soup.find_all('p')
    print str(p) + '\n' + '\n'
1个回答

5
问题在于页面上有多个class为“bd”的div标签。看起来你需要包含实际文章的那一个 - 它在article标签内部:
import urllib2
from bs4 import BeautifulSoup

# Ask user to enter URL
url = raw_input("Please enter a valid URL: ")

soup = BeautifulSoup(urllib2.urlopen(url))

# retrieve all of the paragraph tags
paragraphs = soup.find('article').find("div", {'class': 'bd'}).find_all('p')
for paragraph in paragraphs:
    print paragraph.text

打印:

Libyan government forces on Monday seized a North Korea-flagged tanker after...
...

希望这有所帮助。

+1:几乎要发表我的变体,但你先发了。不过,在我的代码中,我必须在print行中添加encode("utf-8")。唯一的区别是我使用的是requests而不是urllib2 - WGS
@Nanashi 谢谢,通常我也更喜欢使用“requests”,但是OP使用的是“urllib2” - 所以决定让代码接近于OP提供的内容。 - alecxe
谢谢,这个很好用!顺便问一下,为什么你更喜欢使用 requests?我是 Python 新手,所以现在想尽可能多地学习。 - user3285763
我做了修改。现在代码不会因为引号而出错了。 - user3285763

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接