【问题标题】:Extracting dates from span using Python Selenium使用 Python Selenium 从跨度中提取日期
【发布时间】:2017-06-22 11:57:02
【问题描述】:

我有这个页面:

Tripadvisor

对于每条发表的评论,标题属性中都有对应的日期,

检查一下:

<span class="ratingDate relativeDate" title="4 February 2017">Reviewed yesterday </span>

因此,对于发布的每条评论,标题属性中都有一个日期,我的问题是我无法从评论中获取所有日期。

我尝试使用此代码:

def Dates():
datediv = driver.find_elements_by_css_selector('div > div.col2of2 > div > div.wrap > div.rating.reviewItemInline > span.ratingDate.relativeDate')
dateatt = datediv.get_Attribute("title")
for date in dateatt:
    print(date.text)

但它仍然不起作用,我得到了错误

AttributeError: 'list' object has no attribute 'get_Attribute'

我哪里错了?

编辑 好的,现在我已经从每个页面中抓取了用户名、日期、标题和整个评论,但是,仅在 IDLE 中。我想把从每一页抓取的数据说成一个字典,然后把它导出成 json 或者直接把它放到一个 excel 表中。

使用字典的方法非常令人困惑,因为我真的不明白如何使用值独立更新不同的键。

content = {}

def mainfunction():
#Hotel Name
hname = driver.find_element_by_id('HEADING').text


#User Names
usernames = driver.find_elements_by_class_name('scrname')
for 

#Dates
datediv = driver.find_elements_by_css_selector('div > div.col2of2 > div > div.wrap > div.rating.reviewItemInline > span.ratingDate.relativeDate')


#Review Title
titlesdiv = driver.find_elements_by_class_name('isNew')
#for titles in titlesdiv:
#print(titles.find_element_by_class_name('noQuotes').text)


#Reviews 
linkdiv = driver.find_element_by_class_name('expandLink')
linkspan = linkdiv.find_element_by_class_name('ulBlueLinks')
linkspan.click()

try:
    WebDriverWait(driver,10).until(ec.presence_of_element_located((By.CLASS_NAME,"no_padding")))
    close1 = driver.find_element_by_css_selector('body > div> span > div.ui_close_x')
    close1.click()

except TimeoutException:
    print ("Loading took too much time!")


reviews = driver.find_elements_by_css_selector(' div > div.col2of2 > div > div.wrap > div > div > p')
for review in reviews:
    print(review.text)


#push the contents to the dictionary



#Move to next page
nextpage()


#To follow successive pages and scrape the content
def nextpage():
    nextpage = driver.find_element_by_css_selector('#REVIEWS >   div.deckTools.btm.test > div >   a.nav.next.rndBtn.ui_button.primary.taLnk').click()
    try:
        WebDriverWait(driver,10).until(ec.presence_of_element_located((By.CLASS_NAME,"pcb")))
        close2 = driver.find_element_by_class_name('ui_close_x')
    close2.click()
    except TimeoutException:
        print ("Loading took too much time!")

    mainfunction()

【问题讨论】:

    标签: python selenium selenium-webdriver automated-tests


    【解决方案1】:

    datediv 是列表。您需要对其进行迭代

    datediv = driver.find_elements_by_css_selector('div > div.col2of2 > div > div.wrap > div.rating.reviewItemInline > span.ratingDate.relativeDate')
    for dateatt in datediv:
        print(dateatt.get_attribute("title"))
    

    【讨论】:

    • 我的错,我纠正了它,但我仍然得到它的属性错误。
    • 现在,我得到 AttributeError: 'WebElement' object has no attribute 'get_Attribute'
    • @rajshastri 应该是get_attribute,小写“a”
    • 我还有一个疑问,我抓取了这个特定页面的内容,比如酒店名称、用户名以及相应的评论标题、日期和评论。现在我想要的是转到下一个评论页面并再次抓取内容。我很困惑,因为没有 goto 语句,所以我可以转到下一页并一次又一次地调用我的函数
    • @rajshastri 您需要找到next 按钮并单击它。类似driver.find_element_by_css_selector(next_button_locator).click()
    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多