刮写温度,位置和时间然后写入csv问题

时间:2018-03-16 15:11:32

标签: python selenium web-scraping writing

我正在为温度,时间和地点抓一个楼宇自动化系统。我从iframe和cmd行获取信息没有问题,但是将它转到csv文件我不能。我也希望将数据放到列和行。我正在使用的基本代码在这里,但我找不到任何类似于我的东西来帮助我。系统也很慢地响应请求所有内容之间的睡眠原因。这只是我开始时的3个点,大约有30个我必须刮掉。

这应该是它看起来像(cmd线):

Space: Lower Hall,
Temperature:  19.6 ºC,
Timestamp: 1521204222910

这就是我得到的(csv输出):

<function data1 at 0x000001CC46775730>
<function data1 at 0x000001CC4ECA89D8>
<function data1 at 0x000001CC46775730>

基本代码

iframes = driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(iframes[1])

time.sleep(5)

while True:

    time.sleep(5)

        def data1():
            element=driver.find_element_by_id("root.content.BoundLabel12") 
            print("Space: Lower Hall","\nTemperature: ", element.text)
            temperature = element.text
            when_updated=driver.find_element_by_name("_lastUpdated")
            timestamp=when_updated.get_attribute("value")
            print("Timestamp:", timestamp)
            print("") #space out on cmd line

    time.sleep(1)

        def data2():

            element=driver.find_element_by_id("root.content.BoundLabel1") 
            print("Space: Den\Bedroom 3","\nTemperature: ", element.text)
            when_updated=driver.find_element_by_name("_lastUpdated")
            timestamp=when_updated.get_attribute("value")
            print("Timestamp:", timestamp)
            print("")

    time.sleep(1)

        def data3():

            element=driver.find_element_by_id("root.content.BoundLabel33") 
            print("Space: Lower Bathroom" , "\nTemperature: ", element.text)
            date = when_updated=driver.find_element_by_name("_lastUpdated")
            timestamp=when_updated.get_attribute("value")
            print("Timestamp:", timestamp)
            print("")

    time.sleep(60)

        data = ([data1()])

        with open('writer.csv', 'a') as file:
            writer = csv.writer(file)
            writer.writerows(data())

    nos=1  # Number of samples
    freq=25  # Sampling frequency
    print("\nTemperature & timestamp, sampled every", freq, "seconds:")

    while True:
    #for i in range(nos):
        ts=driver.find_element_by_name("_lastUpdated").get_attribute("value")
        #print("\nTemperature: ", element.text, "read at:", ts)
        time.sleep(freq)

    driver.switch_to_default_content()

    print ("Done.")

1 个答案:

答案 0 :(得分:1)

你缺少一些python的基础知识。首先,如果你发现你正在编写许多做同样事情的函数,你可以而且应该把它写成一个函数。其次,这就是您的问题出现的地方,您不是在您的函数中返回任何数据。它打印出你的结果,因为打印不需要返回工作,但是当你尝试将这些函数写入csv时,由于它们没有返回任何内容,它只是编写该函数。这些对您的代码的编辑解决了我的上述观点:

time.sleep(5)

data = []

def get_data(id)
    time.sleep(1)
    element=driver.find_element_by_id(id) 
    print("Space: Lower Hall","\nTemperature: ", element.text)
    temperature = element.text
    when_updated=driver.find_element_by_name("_lastUpdated")
    timestamp=when_updated.get_attribute("value")
    print("Timestamp:", timestamp)
    print("") #space out on cmd line  
    return temperature, when_updated, timestamp      



data.append(get_data("root.content.BoundLabel12"))
data.append(get_data("root.content.BoundLabel1"))
data.append(get_data("root.content.BoundLabel33"))


time.sleep(60)


with open('writer.csv', 'a') as file:
    writer = csv.writer(file)
    for d in data:
        writer.writerows(d)

nos=1  # Number of samples
freq=25  # Sampling frequency
print("\nTemperature & timestamp, sampled every", freq, "seconds:")

while True:
#for i in range(nos):
    ts=driver.find_element_by_name("_lastUpdated").get_attribute("value")
    #print("\nTemperature: ", element.text, "read at:", ts)
    time.sleep(freq)

driver.switch_to_default_content()

print ("Done.")

如果你是python的新手,我建议给你一个读取: https://www.tutorialspoint.com/python/python_functions.htm