2017-04-05 26 views
2

this pageからいくつかのデータを抽出しようとしています。私は2つの文字列間にテキストを抽出したいと思います(Item 1AリスクファクトリとItem 1B UNRESOLVED STAFF COMMENTS)。それを行うために正しい正規表現を思いつくのは難しいです。HTMLページ(Python)からのデータの抽出

import re 
import html2text 

url = "https://www.sec.gov/Archives/edgar/data/104169/000010416916000079/wmtform10-kx1312016.htm" 
html = urllib.urlopen(url).read() 

text = html2text.html2text(html) 

regex= '(?<=Item 1A Risk Factors)(.*)(?=Item 1B Unresolved)' 

match = re.search(regex, text, flags=re.IGNORECASE) 

print match 

上記のコードは「なし」を返します。助言がありますか?

+1

正規表現でHTMLを解析しないでください。実際のパーサでCSSセレクタまたはXpathを使用できますか? – jonrsharpe

+0

htmlソースに「Item 1A Risk Factors」や「Item 1B Unresolved」という文字列は含まれていません。 – horcrux

+0

「Item 1A Risk Factors」または「Item 1B Unresolved」は実際のテキストに記載されています。だから私はまずhtmlタグを削除し、正規表現を使用しようとしています。これが理にかなってほしい。 – kevin

答えて

2

regExを使用する場合、Python 3.5.2で動作する以下のコードを使用できます。 "テキスト"を印刷して、ウェブページに表示されているITEM 1Aの実際の値(ITEM \ &#160 \; 1A)を確認してください。お役に立てれば。何と交換

<(?:(?:(?:(script|style|object|embed|applet|noframes|noscript|noembed)(?:\s+(?>"[\S\s]*?"|'[\S\s]*?'|(?:(?!/>)[^>])?)+)?\s*>)[\S\s]*?</\1\s*(?=>))|(?:/?[\w:]+\s*/?)|(?:[\w:]+\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]?)+\s*/?)|\?[\S\s]*?\?|(?:!(?:(?:DOCTYPE[\S\s]*?)|(?:\[CDATA\[[\S\s]*?\]\])|(?:--[\S\s]*?--)|(?:ATTLIST[\S\s]*?)|(?:ENTITY[\S\s]*?)|(?:ELEMENT[\S\s]*?))))>

を ""

を次に上でこれを実行してあなただけのこの

検索してhtmlタグを削除することができ

import urllib.request 
from urllib.error import URLError, HTTPError 
import re 
import contextlib 

mainpage = "https://www.sec.gov/Archives/edgar/data/104169/000010416916000079/wmtform10-kx1312016.htm" 

try: 
    with contextlib.closing(urllib.request.urlopen(mainpage)) as url: 
     htmltext = url.read().decode('utf-8') 
     #print(htmltext) 
except HTTPError as e: 
    print("HTTPError") 
except URLError as e: 
    print("URLError") 
else: 
    results = re.findall(r'(?=ITEM\&\#160\;1A\.(.*)(RISK FACTORS))(.*)(?=ITEM\&\#160\;1B\.(.*)(UNRESOLVED))',htmltext) 
    print (results) 
+0

ありがとう! @anonyXmous – kevin

1

結果の文字列

何がしたいことは右、あなたがあなた自身のアプリでテキストを折り返すことができたり、

http://www.regexformat.comアプリ
文書にグループ1の文字列を貼り付けキャプチャグループに1

ある

1A\s*\.\s*RISK\s+FACTORS(.*?)1B\s*\.\s*UNRESOLVED\s+STAFF\s+COMMENTS

コンテキストメニュー - >その他のユーティリティ - >ワードラップをクリックします。
最大行の長さに約60の値を入力します。

そして、以下のように(これは切り捨てられています)、5kのラップされたテキストがポップアウトされます。

The risks described below could materially and adversely 
affect our business, results of operations, financial 
condition and liquidity. Our business operations could also 
be affected by additional factors that apply to all 
companies operating in the U.S. and globally.Strategic 
RisksGeneral or macro-economic factors, both domestically 
and internationally, may materially adversely affect our 
financial performance.General economic conditions, globally 
or in one or more of the markets we serve, may adversely 
affect our financial performance. Higher interest rates, 
lower or higher prices of petroleum products, including 
crude oil, natural gas, gasoline, and diesel fuel, higher 
costs for electricity and other energy, weakness in the 
housing market, inflation, deflation, increased costs of 
essential services, such as medical care and utilities, 
higher levels of unemployment, decreases in consumer 
disposable income, unavailability of consumer credit, higher 
consumer debt levels, changes in consumer spending and 
shopping patterns, fluctuations in currency exchange rates, 
higher tax rates, imposition of new taxes and surcharges, 
other changes in tax laws, other regulatory changes, overall 
関連する問題