我正在处理这一守则:
from bs4 import BeautifulSoup
import glob
import os
import re
def trade_spider():
os.chdir(r"C:\Users\6930p\FLO'S DATEIEN\Master FAU\Sommersemester 2016\02_Masterarbeit\04_Testumgebung\01_Probedateien für Analyseaspekt\Independent Auditors Report")
for file in glob.glob('*.html'):
with open(file, encoding="utf8") as f:
contents = f.read()
soup = BeautifulSoup(contents, "html.parser")
results = [item for item in soup.findAll("ix:nonfraction") if re.match("^[^:]:AuditFeesExpenses", item['name'])]
print(results)
#print(file, end="| ")
#print(item['name'], end="| ")
#print(item.get_text())
trade_spider()我试图用BS4解析计算机上某个目录中的多个HTML文档。我的目标是找到以“ix:NonFraction.”开头的标签。包含一个名称属性,可以在'AuditFeesExpenses‘之前有几个表达式,比如name=“name=:AuditFeesExpenses、name=bus:AuditFeesExpenses”等等(这就是我使用RegEx的原因)。因此,如果BS4找到了特定的标记,我想用soup.get_text(Value)从其中提取文本。
有人知道我错过了什么吗?
更新:一个示例标记是:
<td style=" width:12.50%; text-align:right; " class="ta_60">
<ix:nonFraction contextRef="ThirdPartyAgentsHypercube_FY_31_12_2012_Set1"
name="ns19:AuditFeesExpenses" unitRef="GBP" decimals="0"
format="ixt2:numdotdecimal" scale="0" xmlns:ix="http://www.xbrl.org
/2008/inlineXBRL">3,600</ix:nonFraction></td>通常,这个标记出现在一行中,因为清楚的原因,我插入了几行新行!
我的最后代码如下:
from bs4 import BeautifulSoup
import glob
import os
import re
def trade_spider():
os.chdir(r"C:\Users\6930p\FLO'S DATEIEN\Master FAU\Sommersemester 2016\02_Masterarbeit\04_Testumgebung\01_Probedateien für Analyseaspekt\Independent Auditors Report")
for file in glob.glob('*.html'):
with open(file, encoding="utf8") as f:
contents = f.read()
soup = BeautifulSoup(contents, "html.parser")
for item in soup.findAll("ix:nonfraction"):
if re.match(".*AuditFeesExpenses", item['name']):
print(file, end="| ")
print(item['name'], end="| ")
print(item.get_text())
trade_spider()给我这个输出:
Prod224_0010_00079350_20140331.html|英国-金色:审计费用体验
发布于 2016-05-10 17:59:54
findAll()函数的第一个参数是name。当你打电话的时候
`soup.findAll('ix:NonFraction', name=re.compile("^[^:]:AuditFeesExpenses"))`, 您实际上是使用参数name=ix:NonFraction 和 name=re.compile("^[^:]:AuditFeesExpenses")调用。当然,我们只能将name设置为这两个输入中的一个,从而产生错误。
错误消息显示的是find_all()而不是findAll()。从文档中可以看出,findAll是find_all的旧方法名。应该使用find_all方法。
混淆可能来自属性name。区分BeautifulSoup属性name和html属性name非常重要。为了演示,我假设标记具有以下格式:
<body>
<ix:NonFraction name="AuditFeesExpenses">stuff<ix:NonFraction>
</body>我们可以用<ix:NonFraction>找到所有的soup.find_all("ix:nonfraction")标记。它给出了包含结果的下列列表:
[<ix:NonFraction name="AuditFeesExpenses">stuff<ix:NonFraction>]迭代这个单项列表,查看两个不同的名称属性。首先,我们将BeautifulSoup名称属性作为对象的属性访问:
for item in soup.find_all("ix:nonfraction"):
print(item.name)
Out: 'ix:nonfraction'要查看html name属性,请将name作为字典键访问:
for item in soup.find_all("ix:nonfraction"):
print(item['name'])
Out: 'AuditFeesExpenses'将这两个搜索结合在一起,缩小搜索结果范围:
results = [item for item in soup.find_all("ix:nonfraction") if re.match("^[^:]:AuditFeesExpenses", item['name'])
Out: [<ix:nonfraction name="ns19:AuditFeesExpenses">3,600</ix:nonfraction>]或者,如果我们想得到每个匹配的文本:
results = [item.get_text() for item in soup.find_all("ix:nonfraction") if re.match("^[^:]:AuditFeesExpenses", item['name'])
Out: [3,600]建议的完整输出代码:
from bs4 import BeautifulSoup
import glob
import os
def trade_spider():
os.chdir(r"C:\Independent Auditors Report")
for file in glob.glob('*.html'):
with open(file, encoding="utf8") as f:
contents = f.read()
soup = BeautifulSoup(contents, "html.parser")
for item in soup.findAll("ix:nonfraction"):
if re.match("^[^:]:AuditFeesExpenses", item['name'])
print(file, end="| ")
print(item['name'], end="| ")
print(item.get_text())
trade_spider()https://stackoverflow.com/questions/37144234
复制相似问题