所以,我有一个html文件,看起来像这样得到最后一个项目:的Python(汤):获取嵌套的数据,并在标签
<title>Speaker Name: Title of Talk | Subtitle | website.com</title>
... [Other Stuff]
<div class='meta'><span class='meta__item'>
Posted
<span class='meta__val'>
Jun 2006
</span></span><span class='meta__row'>
Rated
<span class='meta__val'>
Funny, Informative
</span></span></div>
<div class='talk-article__body talk-transcript__body'> TEXT
<data class='talk-transcript__para__time'>15:57</data>
我有2200个文件这个样子,和我希望把将它们全部转换为具有AUTHOR,TITLE,DATE,LENGTH和TEXT列的CSV文件。现在,我有什么是不是最漂亮的代码,但它的工作原理:
from bs4 import BeautifulSoup as soup
soup = soup(open(file).read(), "lxml")
at = soup.find("title").text
author = at[0:at.find(':')]
title = at[at.find(":")+1 : at.find("|") ]
text = soup.find("div", attrs={ "class" : "talk-article__body"}) # still needs cleaning
date =
length =
我不能为我的生活弄清楚如何在日期得到:我怀疑它是soup
和re
组合,但我承认,我无法将自己的头围绕在组合上。
长度的诀窍是,我想找到的是上次时间<data class='talk-transcript__para__time'>
发生在文件中并抓住THAT值。
这是优秀!谢谢! –
@JohnLaudun欢迎你 – rock321987