使用BeautifulSoup获取标记内的所有内容

时间:2017-07-13 10:17:49

标签: python web-scraping beautifulsoup

我试图获取文章标记中的所有内容,让我们说http://magazine.magix.com/de/5-tipps-fuer-die-fotobearbeitung/

但是,使用时

print soup.article

它只会走到“... ... auf verschiedene Art und WeiseundfürverschiedeneZwecke bearbeiten。”

整个代码:

from bs4 import BeautifulSoup
import requests

request_page = requests.get('http://magazine.magix.com/de/5-tipps-fuer-die-fotobearbeitung/', 'html.parser')
source = request_page.text
soup = BeautifulSoup(source, "html.parser")
print soup.article.text

我怎样才能得到一切?

1 个答案:

答案 0 :(得分:4)

好的,终于找到了。欢迎来到令人惊叹的刮刮世界。

<article>代码中,存在一些</br>代码,该代表肯定意为<br/>

无论如何,它打破了html流程,因此BS努力解析它。

以下是我解决它的方法:

from bs4 import BeautifulSoup
import requests

request_page = requests.get('http://magazine.magix.com/de/5-tipps-fuer-die-fotobearbeitung/', 'html.parser')
source = request_page.text
source = source.replace('</br>', '<br/>')
soup = BeautifulSoup(source, "html.parser")
print soup.article

(我将</br>替换为<br/> ...)

这是一个很好的刮痧,这种东西很多,依赖于:)