我有很多html文件,我必须使用文件的完整标题。 标头的标签位置不同:class =“ c6”,class =“ c7”
我尝试过BeautifulSoup
for head_c6 in soup.find_all('span', attrs={'class': 'c6'}):
print(head_c6.get_text())
for head_c7 in soup.find_all('span', attrs={'class': 'c7'}):
print(head_c7.get_text())
但结果:
2017年第三季度美国运通公司收益电话-最终长度:
2016年第二季度Akamai Technologies Inc通话-最终收入
以下是不同文件的外观:
文件1
<div class="c4">
<p class="c5">
<span class="c6">
Q3 2017 American Express Co Earnings Call - Final
</span>
</p>
</div>
<div class="c4">
<p class="c5">
<span class="c7">
LENGTH:
</span>
<span class="c2">
11051 words
</span>
</p>
</div>
文件2
<div class="c4">
<p class="c5">
<span class="c6">
Q2 2018 Akamai Technologies Inc
</span>
<span class="c7">
Earnings
</span>
<span class="c6">
Call - Final
</span>
</p>
</div>
文件3
<div class="c4">
<p class="c5">
<span class="c6">
Q4 2018
</span>
<span class="c7">
Facebook
</span>
<span class="c6">
Inc
</span>
<span class="c7">
Earnings
</span>
<span class="c6">
Call - Final
</span>
</p>
我想要的是获取标题的全文:
2017年第三季度美国运通公司收益电话-最终
2018年第二季度Akamai Technologies Inc收益电话-最终
2018年第四季度Facebook Inc收入电话-最终
答案 0 :(得分:0)
使用正则表达式re
我更新了最后一个文件html。您可以对其余文件进行同样操作
from bs4 import BeautifulSoup
import re
data='''<div class="c4">
<p class="c5">
<span class="c6">
Q4 2018
</span>
<span class="c7">
Facebook
</span>
<span class="c6">
Inc
</span>
<span class="c7">
Earnings
</span>
<span class="c6">
Call - Final
</span>
</p>'''
soup=BeautifulSoup(data,'html.parser')
items=[item.text.strip() for item in soup.find_all('span', class_=re.compile("c"))]
stritem=' '.join(items)
print(stritem.replace('\n',''))
输出:
Q4 2018 Facebook Inc Earnings Call - Final
您还可以使用以下方式。
items=[item.text.strip() for item in soup.find_all('span', class_=re.compile("c6|c7"))]
stritem=' '.join(items)
print(stritem.replace('\n',''))
或者要获取父标签文本,请尝试
。from bs4 import BeautifulSoup
import re
data='''<div class="c4">
<p class="c5">
<span class="c6">
Q4 2018
</span>
<span class="c7">
Facebook
</span>
<span class="c6">
Inc
</span>
<span class="c7">
Earnings
</span>
<span class="c6">
Call - Final
</span>
</p>'''
soup=BeautifulSoup(data,'html.parser')
childtag=soup.find('span', class_=re.compile("c6|c7"))
parenttag=childtag.parent
print(parenttag.text.replace('\n',''))
答案 1 :(得分:0)
尝试一下:
使用类顶部:
from bs4 import BeautifulSoup
html1 = ''' <div class="c4">
<p class="c5">
<span class="c6">
Q4 2018
</span>
<span class="c7">
Facebook
</span>
<span class="c6">
Inc
</span>
<span class="c7">
Earnings
</span>
<span class="c6">
Call - Final
</span>
</p></div>'''
soup = BeautifulSoup(html1,'lxml')
tag = soup.find('div',{'class':'c4'})
header = ' '.join(("".join((tag.text.strip()).split('\n'))).split())
print(header)
O / P
2018年第四季度Facebook Inc收益电话-最终
答案 2 :(得分:0)
传递“或”列表进行选择似乎更容易,更有效
from bs4 import BeautifulSoup as bs
html = '''<div class="c4">
<p class="c5">
<span class="c6">
Q4 2018
</span>
<span class="c7">
Facebook
</span>
<span class="c6">
Inc
</span>
<span class="c7">
Earnings
</span>
<span class="c6">
Call - Final
</span>
</p>'''
soup= bs(html,'html.parser')
result = ' '.join([item.text.strip() for item in soup.select('.c6,.c7')])
print(result)