考虑到“最近评论”一节这里下的明星评级,
我正试图建立一个列表,每个评论显示在页面上的明星评级。问题是,每个星等对象都没有一个值。例如,我可以通过这样的xpath获得一个单独的星型对象:
from splinter import Browser
url = 'https://www.greatschools.org/texas/harker-heights/3978-Harker-Heights-Elementary-School/'
browser.visit(url)
astar=browser.find_by_xpath('/html/body/div[5]/div[4]/div[2]/div[11]/div/div/div[2]/div/div/div[2]/div/div[2]/div[3]/div/div[2]/div[1]/div[2]/span/span[1]')问题是,我似乎无法访问对象星体的值(填不填)。
下面是HTML:
<div class="answer">
<span class="five-stars">
<span class="icon-star filled-star"></span>
<span class="icon-star filled-star"></span>
<span class="icon-star filled-star"></span>
<span class="icon-star filled-star"></span>
<span class="icon-star filled-star"></span>
</span>
</div>更新:一些评论根本没有明星评级,所以我需要能够确定一个特定的评论是否有明星评级,如果有,评级是什么。这似乎至少对获得一份所有恒星的名单很有帮助。我用它来做这个:
stars = browser.find_by_css('span[class="icon-star filled-star"]')因此,如果我能得到一个列表,显示如果一个评论有一个明星评级(类似于评级=1,0-1,1.)的顺序。以及所有恒星的序列(即‘填充’,‘填充’,‘空’),我想我可以把序列拼凑在一起。
发布于 2018-06-21 18:41:14
一个解决方案:访问每个对象的html属性,如下所示:
#Get total number of comments
allcoms = len(browser.find_by_text('Overall experience'))
#Loop through all comments and gather into list
comments = []
#If pop-up box occurs, use div[4] instead of second div[5]
if browser.is_element_present_by_xpath('/html/body/div[5]/div[4]/div[2]/div[11]/div/div/div[2]/div/div/div[2]/div/div[2]/div[1]/div/div[2]'):
use='4'
else:
use='5'
for n in range(allcoms): #sometimes the second div[5] was div[4]
comments.append(browser.find_by_xpath('/html/body/div[5]/div['+use+']/div[2]/div[11]/div/div/div[2]/div/div/div[2]/div/div[2]/div['+str(n+1)+']/div/div[2]').value)
#Get all corresponding star ratings
#https://stackoverflow.com/questions/46468030/how-select-class-div-tag-in-splinter
ratingcode = []
ratings = browser.find_by_css('span[class="five-stars"]')
for a in range(len(comments)+2): #Add 2 to skip over first 2 ratings
if a<2: #skip first 2 and last 3 because these are other ratings - by just using range(len(comments)) above to get correct # before stopping
pass
else:
ratingcode.append(ratings[a].html)https://stackoverflow.com/questions/50970254
复制相似问题