2017-08-07 7 views
-2

私は、動物病院のCSVファイルを状態別に作成したいと考えています。私はhtmlの選択が間違っていると思う。私は、状態、名前、住所、電話番号を解析するために正しいタグを選択する要素を反復したいと思います。HTMLの構文解析要素

from lxml import html 
import requests 

link = "https://vcahospitals.com/find-a-hospital/location-directory" 
response = requests.get(link, allow_redirects = False) #get page data from server, block redirects 
sourceCode = response.content #get string of source code from response 
htmlElem = html.document_fromstring(sourceCode) #make HTML element object 

print(sourceCode) 

[例ページhtml。私は、[1]

I would think this grabs all the state hospitals, but it only prints out one state's worth

答えて

1

あなたは間違ったコード内のprint文をインデントしている]クラスとして、すべてのdiv要素を選択しようとしました。

for el in state_hospitals: 
    text = el.text_content() 

    # indented in the for block. 
    print (text) 
+0

awesome!それが助けになりました。だからシンプルな(私はまた私のdivをchaningで働くようになった)あなたは列の分離についてのアイデアを持っていますか:状態、住所、電話などforループ内?再度、感謝します!私はそれを感謝する@achachin –

+0

あなたの問題を解決した場合は、この答えを正しいと受け入れることができます。それ以外にも、文字列操作を使用してその作業を行うことができます。 –