我遇到了一个关于 BeautifulSoup 的 python 编程问题。
首先,我需要创建一个函数来从网页的源页面中提取所有标签。我这样做如下:
from bs4 import BeautifulSoup
soup=BeautifulSoup(''.join(data))
def parseUsingSoup(content):
return soup.findAll('h3')
我要解析的网站是这个:http://www.auc.nl/news-events/events-and-lectures/events-and-lectures.html?page=1&pageSize=40
它只包含一个 h3 标签。现在问题要我扩展我的功能,这样它也将在 p-tags 中返回与其相关的所有内容。它还要求提供包含四个元组的事件列表,这些元组给出了事件的日期、标题、类型和描述。
我真的不知道该怎么做。我尝试了各种不同的东西,但没有给我正确的结果。提前谢谢你。
最佳答案
这是获取所有 <p>
的一种方法<h3>
下方的标签:
from bs4 import BeautifulSoup
import urllib2
content = 'http://www.auc.nl/news-events/events-and-lectures/events-and-lectures.html?page=1&pageSize=40'
soup = BeautifulSoup(urllib2.urlopen(content))
for x in soup.findAll('h3'):
for y in soup.findAll('p'):
print y
然后您可以将此输出解析为您认为合适的列表。
关于python - 使用 BeautifulSoup 解析标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703256/