私はこのコードで働いています:美しいスープ4つのHTMLドキュメントディレクトリ
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 ...."で始まるタグを、name = "aurep:AuditFeesExpenses、name = bus:AuditFeesExpenses"などのような 'AuditFeesExpenses'の前にいくつかの式を持つことができる名前属性を含むタグを見つけることです。 RegExを使用しています)。 BS4がその特定のタグを見つけたら、soup.get_text(Value)でテキストを抽出したいと思います。
誰かが私が逃したアイデアですか?
UPDATE: 例タグは次のようになります。
<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>
通常、このタグは、私はいくつかの改行を挿入明瞭の理由から、ONE行に表示されます! |
Prod224_0010_00079350_20140331.html:
私の最終的なコードは次のようになります。
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()
を、私に、この出力を提供しますuk-aurep:AuditFeesExpenses | 2,000
あなたは私は私のコード –
更新の答えを使用しようとしています例タグを見ることができるように、私は私の質問を更新しました。私は問題が2つの異なる 'name'属性に由来すると思います。最終的な解決策は、2つのステップが必要な場合があります。すべての 'NonFraction'タグを取得し、' AuditFeesExpenses'名を取得するためにフィルタリングします。 – SNygard
これはほぼ完璧に動作しますが、pythonは文書内のすべての非分数タグ名を出力します(各文書について〜100-200)。 "AuditFeesExpenses"だけをフィルタリングし、同時にPythonにタグ> 3,600 <>の間にテキストを集めるように伝える機会はありますか?私がこの問題を解決できれば、コードは完全に機能します! –