2016-05-10 18 views
1

私はこのコードで働いています:美しいスープ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

答えて

0

findAll()ファンクションの最初のパラメータはnameです。あなたが効果的にパラメータname=ix:NonFractionname=re.compile("^[^:]:AuditFeesExpenses")soupを呼び出している

`soup.findAll('ix:NonFraction', name=re.compile("^[^:]:AuditFeesExpenses"))`, 

を呼び出すとき。もちろん、nameを2つの入力のうちの1つと同じに設定するだけでエラーが発生します。

findAll()ではなく、find_all()というエラーメッセージが表示されます。 docsから、findAllが古いメソッド名find_allであることがわかります。 find_allメソッドを使用する必要があります。

混乱は、属性nameから来ている可能性があります。 BeautifulSoup属性nameとhtml属性nameを区別することが重要です。

<body> 
    <ix:NonFraction name="AuditFeesExpenses">stuff<ix:NonFraction> 
</body> 

我々はsoup.find_all("ix:nonfraction")ですべて<ix:NonFraction>タグを見つけることができます:実証するために、私はタグがこの形式を持っていることを前提としています。 2つの異なる name属性を表示するには、この1項目のリストを

[<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() 
+0

あなたは私は私のコード –

+0

更新の答えを使用しようとしています例タグを見ることができるように、私は私の質問を更新しました。私は問題が2つの異なる 'name'属性に由来すると思います。最終的な解決策は、2つのステップが必要な場合があります。すべての 'NonFraction'タグを取得し、' AuditFeesExpenses'名を取得するためにフィルタリングします。 – SNygard

+0

これはほぼ完璧に動作しますが、pythonは文書内のすべての非分数タグ名を出力します(各文書について〜100-200)。 "AuditFeesExpenses"だけをフィルタリングし、同時にPythonにタグ> 3,600 <>の間にテキストを集めるように伝える機会はありますか?私がこの問題を解決できれば、コードは完全に機能します! –

関連する問題