2017-09-30 16 views
0

私は以下のコード行を使用して要素のutimeを取得しています。出力から、私は正しい領域をターゲットにしており、そこにutime属性があることがわかりますが、まだ出力はNoneです。 data-utime属性を数回書き直して、関数用に正しくフォーマットされていることを確認しました。私はここで何が欠けていますか?python seleniumを使用して時間データをスクラップする

コード:

timeStampBox = post.find_element_by_css_selector('.fsm.fwn.fcg') 
    timeStampBox = timeStampBox.find_element_by_class_name('_5pcq') 

    print(timeStampBox.get_attribute('innerHTML')) 
    print(timeStampBox.get_attribute('data-utime')) 

出力:

<abbr title="Monday, September 4, 2017 at 6:11am" data-utime="1504530675" data-shorten="1" class="_5ptz"><span class="timestampContent" id="js_15">September 4 at 6:11am</span></abbr> 
None 
+0

あなたのコードは、投稿していないHTMLの束を参照しています。 – JeffC

答えて

1

abbr要素がtimeStampBoxinnerHTMLですがdata-utimetimeStampBoxの属性ではありません。

<html> 
<body> 
<div><abbr title="Monday, September 4, 2017 at 6:11am" data-utime="1504530675" data-shorten="1" class="_5ptz"><span class="timestampContent" id="js_15">September 4 at 6:11am</span></abbr></div> 
</body> 
</html> 

div要素がabbr要素のコンテナです:

は、ここで私はあなたの状況をエミュレートする方法です。私はそれがあなたのtimeStampBox要素であるとふることができます。

>>> from selenium import webdriver 
>>> driver = webdriver.Chrome() 
>>> driver.get('file://c:/scratch/temp.htm') 

timeStampBoxを特定し、そのinnerHTMLを取得します。前と同じように、私はabbr要素を得ました。

>>> timeStampBox = driver.find_element_by_tag_name('div') 
>>> timeStampBox.get_attribute('innerHTML') 
'<abbr title="Monday, September 4, 2017 at 6:11am" data-utime="1504530675" data-shorten="1" class="_5ptz"><span class="timestampContent" id="js_15">September 4 at 6:11am</span></abbr>' 

data-utimeこのプロパティはtimeStampBoxに存在しないためNoneです。

>>> timeStampBox.get_attribute('data-utime') 

しかし、それはabbrにあります。

>>> abbr = driver.find_element_by_tag_name('abbr') 
>>> abbr.get_attribute('data-utime') 
'1504530675' 

私たちの物語のモラル:abbrを直接検索してください。

関連する問題