2017-03-08 9 views
0

私はPythonには新しく、私はpython 2.7で掻き集めたいHTMLテキストファイルを持っています。PythonでHTMLページから特定のデータを抽出する方法は?

以下のコードは、会社の情報の一例です。完全なhtmlテキストファイルでは、コード構造は他のすべての企業でも同じで、相互に位置しています(後者の情報が役立つ場合)。

Liberty Associates LLC | New York | +1 973-344-8300 | www.liberty.edu 
Company B    | Los Angeles | +1 213-802-1770 | perchla.com 

だから基本的に、私は、データは右の組織に割り当てられているので、時系列順に(会社名、所在地、電話番号、ウェブサイトのように)このような何かを特定の情報を抽出したいです

私は簡潔ではないが、スクリプトをどのように起動するか、またどのように表示されるかについての提案は非常に役に立ちます。

コード:

enter image description here

EDIT:

それは、ウェブページ上のように見える方法

<body><div class="tab_content-wrapper noPrint"><div class="tab_content_card"> 
 
      <div class="card-header"> 
 
       <strong title="" d.="" kon.="" nl="">"Liberty Associates LLC"</strong> 
 
       <span class="tel" title="Phone contacts">Phone contacts</span> 
 
\t \t \t 
 
      </div> 
 
      <div class="card-content"> 
 
       
 
\t \t \t \t 
 
       <table> 
 
        <tbody> 
 
         <tr> 
 
          <td colspan="4"> 
 
           
 
           <label class="downdrill-sbi" title="Industry: Immigration">Industry: Immigration</label> 
 
          </td> 
 
         </tr> 
 
         <tr> 
 
          <td width="20">&nbsp;</td> 
 
          <td width="245">&nbsp;</td> 
 
          <td width="50">&nbsp;</td> 
 
          <td width="80">&nbsp;</td> 
 
         </tr> 
 
         <tr> 
 
          <td colspan="2"> 
 
59 Wall St</td> 
 
          <td></td> 
 
          <td></td> 
 
         </tr> 
 
         <tr> 
 
          <td colspan="2">NJ 07105&nbsp;&nbsp; 
 
           
 
           <label class="downdrill-sbi" title="New York">New York</label> 
 
          </td> 
 
          <td></td> 
 
          <td></td> 
 
         </tr> 
 
         <tr> 
 
          <td>&nbsp;</td> 
 
          <td>&nbsp;</td> 
 
          <td>&nbsp;</td> 
 
          <td>&nbsp;</td> 
 
         </tr> 
 
         <tr><td>Phone:</td><td>+1 973-344-8300</td><td>Firm Nr:</td><td>KL4568TL</td></tr> 
 
         <tr><td>Fax:</td><td>+1 973-344-8300</td><td colspan="2"></td></tr> 
 
         <tr> 
 
          <td colspan="2"> <a href="http://www.liberty.edu/" target="_blank">www.liberty.edu</a> </td> 
 
          <td>Active:</td> 
 
          <td>Yes</td> 
 
         </tr> 
 
        </tbody> 
 
       </table> 
 
      </div> 
 
      
 

 
     </div></div></body>

だからajputnamの助けを借りて、私は今、これを持っている:

from lxml import html  

str = open('test_html.txt', 'r').read() 
tree = html.fromstring(str) 

name = tree.xpath("/html/body/div/div/div[1]/strong/text()") 
place = tree.xpath("/html/body/div/div/div[2]/table/tbody/tr[4]/td[1]/label/text()") 
phone = tree.xpath("/html/body/div/div/div[2]/table/tbody/tr[6]/td[2]/text()") 
url = tree.xpath("/html/body/div/div/div[2]/table/tbody/tr[8]/td[1]/a/text()") 

print(name, place, phone, url) 

プリント:しかし

(['"Liberty Associates LLC"'], ['New York'], ['+1 973-344-8300'], ['www.liberty.edu']) 

、私は(複数の企業データとの)全体htmlファイルにこのコードを試してみてください私はすべての一致する変数が互いに背後にあることを得ます。 [0]を使用して、このようなデータ構造を正しく取得するにはどうすればよいですか?:

Liberty Associates LLC | New York | +1 973-344-8300 | www.liberty.edu 
Company B    | Los Angeles | +1 213-802-1770 | perchla.com 
+0

から読み取るか、それがウェブページ上に見えるのでしょうか? –

+0

@Radical Fanatic最新の投稿を参照してください – jakeT888

答えて

1

まず、ページからHTMLを取得する必要があります。あなたはこのような要求のようなライブラリを使うことができます。

from lxml import html 
import requests 

page = requests.get('url') 
tree = html.fromstring(page.content) 

次に、セレクタを使用して「ツリー」内のものにアクセスできます。

prices = tree.xpath('//span[@class="item-price"]/text()') 

または、文字列を正常に解析できます。

は、以下を参照してください。HTML scrapping

がファイル

from lxml import html 

# read html as string from file 
str = open('file.html', 'r').read() 
tree = html.fromstring(str) 

company = tree.xpath('//div[@class="card-header"]/strong/text()') 
print company 
+0

返信いただきありがとうございます。残念ながら私はページ/ URLを持っていません。私はHTMLコードを私のHDDのtxtファイルに保存しているだけです。 – jakeT888

+0

ああもっと簡単です。ファイルを文字列として読み込むだけで、同じ手順を使用できます。 – ajputnam

+0

このメソッドは "ループ"して、最初のhtml "ブロック"が終了した時点で他の企業のデータもクロールしますか? – jakeT888

関連する問題