2017-03-11 29 views
-1

私は比較的単純なプロジェクトを行っていますので、私は自分自身にPythonを教えることができます。私は立ち往生しています。だから私は enter image description herePythonの変数から属性値を取得する方法

として示しpycharmデバッガで変数の名前要素を持ってこの変数は、私には正しいタイプのタグ、です。 の場合上記の画像に該当しない場合はclass="schedule_dgrd_time/result"をご覧になりたいです。

の要素の中には、attrsがあります。 enter image description here

どのようにしてその値にアクセスできますか?もし私がelement.stringをすれば、この場合のテキスト値はSatになります。(...私はその仕事をすることができます)が、私はクラスの属性値を最初にチェックすることができますか?

私は数日前からこれを探していて、それを手に入れることはできません。私はこの時点で自分自身を捜した。どのような助けや指針をいただければ幸いです。読んでくれてありがとう。

enter image description here

更新 ここでは私のコードは

import urllib2 
import datetime 
import re 
from bs4 import BeautifulSoup 

# today's date 
date = datetime.datetime.today().strftime('%-m/%d/%Y') 
validDay = "Mon\.|Tue\.|Wed\.|Thu(r)?(s)?\.|Fri\." 
website = "http://www.texassports.com/schedule.aspx?path=baseball" 

opener = urllib2.build_opener() 
##add headers that make it look like I'm a browser 
opener.addheaders = [('User-Agent', 'Mozilla/5.0')] 
page = opener.open(website) 
# turn page into html object 
soup = BeautifulSoup(page, 'html.parser') 
#print soup.prettify() 

#get all home games 
all_rows = soup.find_all('tr', class_='schedule_home_tr') 

# see if any game is today 
# entryForToday = [t for t in all_rows if t.findAll('nobr',text=re.compile('.*({}).*'.format(date)))] 

# hard coding for testing weekend 
entryForToday = [t for t in all_rows if t.findAll('nobr',text=re.compile('3/11/2017'))] 

time = "schedule_dgrd_time/result" 

for elements in entryForToday: 
    for element in elements: 
     #this is where I'm stuck. 
     # if element.attrs: 
     #  print element.attrs['class'][0] 

である私は、あなたがより良い方法を持っている場合、私はそれを聞いてうれしいので、ループのための二重の入れ子になったが、理想的ではありません知っています。ありがとう

+0

attrsに[ 'クラス'] [0]あなたの "schedule_dgrd_game_day_of_week" を与えるその質問ですか? – Monicka

+0

ええ、どうすれば要素からアクセスできますか? 'element.attrs ['class'] [0]'? – villaa19

+0

コードを貼り付けてください。または、印刷画面の代わりに要素Objectを貼り付けてください。 – Monicka

答えて

0

私は把握することができました。私はattrsを持たないNavigableStringをいくつか持っているので、それはエラーを投げていました。 element.attrs['class'][0]は今動作します。 isinstanceOfがタグであるかどうかを確認しなければなりませんでした。もしそうでなければ、それをスキップします。 Anywho、私のコードは、興味がある人のために以下にあります。

import urllib2 
import datetime 
import re 
from bs4 import BeautifulSoup 
from bs4 import Tag 

# today's date 
date = datetime.datetime.today().strftime('%-m/%d/%Y') 
validDay = "Mon\.|Tue\.|Wed\.|Thu(r)?(s)?\.|Fri\." 
website = "http://www.texassports.com/schedule.aspx?path=baseball" 

opener = urllib2.build_opener() 
##add headers that make it look like I'm a browser 
opener.addheaders = [('User-Agent', 'Mozilla/5.0')] 
page = opener.open(website) 
# turn page into html object 
soup = BeautifulSoup(page, 'html.parser') 
#print soup.prettify() 

#get all home games 
all_rows = soup.find_all('tr', class_='schedule_home_tr') 

# see if any game is today 
# entryForToday = [t for t in all_rows if t.findAll('nobr',text=re.compile('.*({}).*'.format(date)))] 

# hard coding for testing weekend 
entryForToday = [t for t in all_rows if t.findAll('nobr',text=re.compile('3/14/2017'))] 

classForTime = "schedule_dgrd_time/result" 
timeOfGame = "none"; 

if entryForToday: 
entryForToday = [t for t in entryForToday if t.findAll('td', 
                 class_='schedule_dgrd_game_day_of_week', 
                 text=re.compile('.*({}).*'.format(validDay)))] 
if entryForToday: 
    for elements in entryForToday: 
     for element in elements: 

      if isinstance(element, Tag): 
       if element.attrs['class'][0] == classForTime: 
        timeOfGame = element.text 
       # print element.text 
        break 

print timeOfGame 
関連する問題