0

Outwitハブを使用して、都市、州、国のWebサイトをスクレイプしています(米国とカナダのみ)。このプログラムでは、正規表現を使用して、取得したいテキストの前後のマーカーを定義できます。希望するテキストのフォーマットを定義することもできます。

これがhtmlのサンプルです:

<td width="8%" nowrap="nowrap"></td>                        
<td width="22%" nowrap="nowrap"><strong>
BILLINGS, MT
USA</strong></td>
<td width="10%" align="right" nowrap="nowrap">

正規表現を設定しました。次のように:

CITY-以前 (正規表現としてフォーマットされていません)

<td width="22%" nowrap="nowrap"><strong>

CITY-After (州、準州、および州を考慮)

/(,\s|\bA[BLKSZRAEP]\b|\bBC\b\bC[AOT]\b|\bD[EC]\b|\bF[LM]\b|\bG[AU]\b|\bHI\b|\bI[ADLN]\b|\bK[SY]\b|\bLA\b|\bM[ABDEHINOPST]\b|\bN[BLTSUCDEHJMVY]\b|\bO[HKNR]\b|\bP[AERW]\b|\bQC\b|\bRI\b|\bS[CDK]\b|\bT[NX]\b|\bUT\b|\bV[AIT]\b|\bW[AIVY]\b|\bYT\b|\bUSA|\bCanada)/

状態-前

\<td width="22%" nowrap="nowrap"\>\<strong\>\s|,\s

状態-後

/\bUSA\<\/strong\>\<\/td\>|\bCanada\<\/strong\>\<\/td\>/

STATE-フォーマット

/\b[A-Z][A-Z]\b/

国-以前 (州、準州、および州を考慮)

/(\bA[BLKSZRAEP]\b|\bBC\b\bC[AOT]\b|\bD[EC]\b|\bF[LM]\b|\bG[AU]\b|\bHI\b|\bI[ADLN]\b|\bK[SY]\b|\bLA\b|\bM[ABDEHINOPST]\b|\bN[BLTSUCDEHJMVY]\b|\bO[HKNR]\b|\bP[AERW]\b|\bQC\b|\bRI\b|\bS[CDK]\b|\bT[NX]\b|\bUT\b|\bV[AIT]\b|\bW[AIVY]\b|\bYT\b)\s/

国-後 (正規表現としてフォーマットされていません)

</strong></td><td width="10%" align="right" nowrap="nowrap">

この問題は、市または州がリストされていない場合に発生します。私はこれを説明しようとしましたが、それを悪化させています。これをクリーンアップしても、情報が欠落している可能性を説明する方法はありますか?ありがとうございました。

都市のない例:

<td width="8%" nowrap="nowrap"></td>                        
<td width="22%" nowrap="nowrap"><strong>
MT
USA</strong></td>
<td width="10%" align="right" nowrap="nowrap">

都市/州がない例:(はい、余分な改行があります)

<td width="8%" nowrap="nowrap"></td>                        
<td width="22%" nowrap="nowrap"><strong>

USA</strong></td>
<td width="10%" align="right" nowrap="nowrap">

あなたが提供できるどんな助けにも感謝します。

4

2 に答える 2

1

プロ版をお持ちの場合は、次のことができます。

Description: Data
Before: <td width="22%" nowrap="nowrap"><strong>
After: </strong>
Format: (([\w \-]+),)? ?([A-Z]{2})?[\r\n](USA|canada)\s*
Replace: \2##\3##\4
Separator: ##
Labels: City,State,Country

軽量バージョンを使用している場合は、次の 3 行で行う必要があります。

Description: City
Before: <td width="22%" nowrap="nowrap"><strong>
After: ,
Format: [^<>]+

Description: State
Before: /<td width="22%" nowrap="nowrap"><strong>[\r\n]([^<>\r\n ]+,)?/
After: /[\r\n]/
Format: [A-Z]{2}

Description: Country
Before:
After: </strong></td>
Format: (USA|canada)
于 2012-05-16T21:28:58.267 に答える
0

TXR テキスト スクレイピング、データ変更言語:

@(collect)
<td width="8%" nowrap="nowrap"></td>
<td width="22%" nowrap="nowrap"><strong>
@  (cases)
@city, @state
@  (or)

@    (bind (city state) ("n/a" "n/a"))
@  (or)
@state
@    (bind city "n/a")
@  (end)
@country</strong></td>
<td width="10%" align="right" nowrap="nowrap">
@(end)
@(output)
CITY       STATE       COUNTRY
@  (repeat)
@{city 10} @{state 11} @country
@  (end)
@(end)

このファイルcity.htmlには、連結されたツリー ケースが含まれています。走る:

$ txr city.txr  city.html
CITY       STATE       COUNTRY
BILLINGS   MT          USA
n/a        MT          USA
n/a        n/a         USA

TXR HTML スクレイピングの別の例: HTML テーブルからテキストを抽出する

于 2012-05-14T19:11:04.263 に答える