首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >漂亮的Soup 4 HTML文档目录

漂亮的Soup 4 HTML文档目录
EN

Stack Overflow用户
提问于 2016-05-10 16:32:46
回答 1查看 715关注 0票数 1

我正在处理这一守则:

代码语言:javascript
复制
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)从其中提取文本。

有人知道我错过了什么吗?

更新:一个示例标记是:

代码语言:javascript
复制
    <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>

通常,这个标记出现在一行中,因为清楚的原因,我插入了几行新行!

我的最后代码如下:

代码语言:javascript
复制
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|英国-金色:审计费用体验

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-10 17:59:54

findAll()函数的第一个参数是name。当你打电话的时候

代码语言:javascript
复制
`soup.findAll('ix:NonFraction', name=re.compile("^[^:]:AuditFeesExpenses"))`, 

您实际上是使用参数name=ix:NonFraction name=re.compile("^[^:]:AuditFeesExpenses")调用。当然,我们只能将name设置为这两个输入中的一个,从而产生错误。

错误消息显示的是find_all()而不是findAll()。从文档中可以看出,findAllfind_all的旧方法名。应该使用find_all方法。

混淆可能来自属性name。区分BeautifulSoup属性name和html属性name非常重要。为了演示,我假设标记具有以下格式:

代码语言:javascript
复制
<body>
    <ix:NonFraction name="AuditFeesExpenses">stuff<ix:NonFraction>
</body>

我们可以用<ix:NonFraction>找到所有的soup.find_all("ix:nonfraction")标记。它给出了包含结果的下列列表:

代码语言:javascript
复制
[<ix:NonFraction name="AuditFeesExpenses">stuff<ix:NonFraction>]

迭代这个单项列表,查看两个不同的名称属性。首先,我们将BeautifulSoup名称属性作为对象的属性访问:

代码语言:javascript
复制
for item in soup.find_all("ix:nonfraction"):
    print(item.name)

Out: 'ix:nonfraction'

要查看html name属性,请将name作为字典键访问:

代码语言:javascript
复制
for item in soup.find_all("ix:nonfraction"):
    print(item['name'])

Out: 'AuditFeesExpenses'

将这两个搜索结合在一起,缩小搜索结果范围:

代码语言:javascript
复制
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>]

或者,如果我们想得到每个匹配的文本:

代码语言:javascript
复制
results = [item.get_text() for item in soup.find_all("ix:nonfraction") if re.match("^[^:]:AuditFeesExpenses", item['name'])

Out: [3,600]

建议的完整输出代码:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37144234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档