Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取BS4中的嵌套数据_Python_Beautifulsoup - Fatal编程技术网

Python 提取BS4中的嵌套数据

Python 提取BS4中的嵌套数据,python,beautifulsoup,Python,Beautifulsoup,我有以下HTML文件,我想提取运行时和视图数据参数。我已经能够导航到main id=videouser类,但我不确定现在如何获取相关文本 vid_data = (soup('td', {'id':'videoUser'}))[0] <td id="videoUser"> <div class="item" style="padding-left: 0;"> <span>Added by</span> <a href="/user/glanc

我有以下HTML文件,我想提取运行时和视图数据参数。我已经能够导航到main id=videouser类,但我不确定现在如何获取相关文本

vid_data = (soup('td', {'id':'videoUser'}))[0]

<td id="videoUser">
<div class="item" style="padding-left: 0;">
<span>Added by</span>
<a href="/user/glanceweb">glanceweb</a>
<a class="hint" hint="Send private message" href="#" onclick="return openPm('glanceweb')" overicon="iconMailOver">
<div class="icon iconMail di" style="margin-bottom:-1px"></div>
</a>
<span class="hint" hint="2013-04-01 01:07:00 UTC">10 months ago</span>
</div>
<div class="item"><span>Runtime:</span> 02:39</div>
<div class="item"><span>Views:</span> 284,397</div>
</td>
vid_data=(soup('td',{'id':'videoUser'))[0]
由添加
10个月前
运行时间:02:39
浏览次数:284397

有人知道如何在BS4中执行此操作吗?

如果您正在查找上述HTML打印的所有文本,则应执行以下操作:

soup = BeautifulSoup(<your-html>)
div = soup.find_all('div', {'class':'item'})[0]
user = str(div.find_all('span')[0].string) + ' ' + str(div.find_all('a')[0].string) + ' ' + str(div.find_all('span')[1].string)
r_div = soup.find_all('div', {'class':'item'})[1]
runtime = r_div.get_text()
v_div = soup.find_all('div', {'class':'item'})[2]
views = v_div.get_text()
运行时将具有:

Added by glanceweb 10 months ago
Runtime: 02:39
届时意见便会有所改变

Views: 284,397