使用 BeautifulSoup 从 `div` 中的 `p` 中提取文本 [英] Extract the text from `p` within `div` with BeautifulSoup

查看:41
本文介绍了使用 BeautifulSoup 从 `div` 中的 `p` 中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 Python 进行网络抓取非常陌生,而且我真的很难从 HTML 中提取嵌套文本(pdiv 中,是精确的).这是我目前得到的:

from bs4 import BeautifulSoup导入 urlliburl = urllib.urlopen('http://meinparlament.diepresse.com/')内容 = url.read()汤 = BeautifulSoup(内容,'lxml')

这很好用:

links=soup.findAll('a',{'title':'zur Antwort'})对于链接中的链接:打印(链接['href'])

此提取工作正常:

table = soup.findAll('div',attrs={"class":"content-question"})对于表中的 x:打印(x)

这是输出:

<p>[...] Die Verhandlungen über die mögliche Visabefreiung fürtürkische Staatsbürger per Ende Ju...<a href="http://meinparlament.diepresse.com/frage/10144/" title="zurAntwort">mehr »</a></p>

现在,我想提取p/p 中的文本.这是我使用的代码:

table = soup.findAll('div',attrs={"class":"content-question"})对于表中的 x:打印(x['p'])

然而,Python 引发了一个 KeyError.

解决方案

以下代码查找并打印 div 中每个 p 元素的文本class "内容问题"

from bs4 import BeautifulSoup导入 urlliburl = urllib.urlopen('http://meinparlament.diepresse.com/')内容 = url.read()汤 = BeautifulSoup(内容,'lxml')table = soup.findAll('div',attrs={"class":"content-question"})对于表中的 x:打印 x.find('p').text# 另一种检索表的方法:# table = soup.select('div[class="content-question"]')

以下是table中第一个p元素的打印文本:

[...] Die Verhandlungen über die mögliche Visabefreiung für türkische Staatsbürger per Ende Juni sind noch nicht abgeschlossen, sodass nicht mit Sicherheit gesagt werden kann, ob es zu diesem ZeitpzuungreiAuch die genauen Modalitäten einer solchen Visaliberalisierung sind noch nicht ausverhandelt.Prinzipiell ist es jedoch so,dass Visaerleichterungen bzw.-liberalisierungen eine Frage von Reziprozität sind, d.h.dass diese für beide Staaten gelten müssten.[...]

I am very new to web-scraping with Python, and I am really having a hard time with extracting nested text from within HTML (p within div, to be exact). Here is what I got so far:

from bs4 import BeautifulSoup
import urllib

url = urllib.urlopen('http://meinparlament.diepresse.com/')
content = url.read()
soup = BeautifulSoup(content, 'lxml')

This works fine:

links=soup.findAll('a',{'title':'zur Antwort'})
for link in links:
    print(link['href'])

This extraction works fine:

table = soup.findAll('div',attrs={"class":"content-question"})
for x in table:
    print(x)

This is the output:

<div class="content-question">
<p>[...] Die Verhandlungen über die mögliche Visabefreiung für    
türkische Staatsbürger per Ende Ju...
<a href="http://meinparlament.diepresse.com/frage/10144/" title="zur 
Antwort">mehr »</a>
</p>
</div>

Now, I want to extract the text within p and /p. This is the code I use:

table = soup.findAll('div',attrs={"class":"content-question"})
for x in table:
    print(x['p'])

However, Python raises a KeyError.

解决方案

The following code finds and prints the text of each p element in the div's with the class "content-question"

from bs4 import BeautifulSoup
import urllib

url = urllib.urlopen('http://meinparlament.diepresse.com/')
content = url.read()
soup = BeautifulSoup(content, 'lxml')

table = soup.findAll('div',attrs={"class":"content-question"})
for x in table:
    print x.find('p').text

# Another way to retrieve tables:
# table = soup.select('div[class="content-question"]')

The following is the printed text of the first p element in table:

[...] Die Verhandlungen über die mögliche Visabefreiung für türkische Staatsbürger per Ende Juni sind noch nicht abgeschlossen, sodass nicht mit Sicherheit gesagt werden kann, ob es zu diesem Zeitpunkt bereits zu einer Visabefreiung kommt. Auch die genauen Modalitäten einer solchen Visaliberalisierung sind noch nicht ausverhandelt. Prinzipiell ist es jedoch so, dass Visaerleichterungen bzw. -liberalisierungen eine Frage von Reziprozität sind, d.h. dass diese für beide Staaten gelten müssten. [...]

这篇关于使用 BeautifulSoup 从 `div` 中的 `p` 中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆