有没有办法让硒只给我今天的数据?

我正在尝试创建一个脚本来告诉我当天的潮汐水平selenium,但它会为我提供整周的所有数据。我怎么让它只给我今天的数据?


from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC 

import time

import datetime

driver = webdriver.Chrome('/Users/judeslater/Downloads/chromedriver')

driver.get('https://magicseaweed.com/Nosara-Surf-Report/445/Tide/')



WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="msw-js-tide-list"]')))



Day_of_Week = datetime.datetime.today().weekday()


Break_Key = Day_of_Week + 1


element = driver.find_elements_by_id('msw-js-tide-list')


intDay = datetime.date(year=2000, month=12, day=1).weekday()

days = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]


def contains_word(string, target):

    return target in string


   


for value in element:

    Ocean_Data = value

    print(Ocean_Data.text)

    time.sleep(1)

    if contains_word(str(Ocean_Data), days[Break_Key]) == True:

       break

    else:

        continue

    break

    


driver.quit()


阿波罗的战车
浏览 140回答 2
2回答

HUH函数

你有很多错误,但主要是你的find_elements_by_id('msw-js-tide-list')它为您提供了所有日期作为一个元素的表格。你应该在最后使用更复杂的xpathwith来将每一天作为单独的元素 - 然后你可以过滤它。/divfind_elements_by_xpath('//*[@id="msw-js-tide-list"]/div')您不必添加+1到Day_of_Week因为python开始索引0如果你想得到明天然后更好地使用tomorrow = today + datetime.timedelta(days=1)0或者当你得到时使用模数移动到7Break_Key = (Day_of_Week + 1) % 7from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC import timeimport datetimedays = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]today = datetime.datetime.today()#tomorrow = today + datetime.timedelta(days=1)#day_of_week = tomorrow.weekday()day_of_week = today.weekday()selected_day = days[day_of_week]driver = webdriver.Chrome('/Users/judeslater/Downloads/chromedriver')#driver = webdriver.Firefox()driver.get('https://magicseaweed.com/Nosara-Surf-Report/445/Tide/')WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="msw-js-tide-list"]')))all_days = driver.find_elements_by_xpath('//*[@id="msw-js-tide-list"]/div')for ocean_data in all_days:    if selected_day in ocean_data.text:        print(ocean_data.text)        breakdriver.quit()如果您想要今天的数据,那么它可能始终是表格中的第一天,因此您all_days[1]无需检查即可使用selected_day隐藏div在 HTML 中,所以我必须使用[1]而不是来[0]获取今天的数据。from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC import timeimport datetimedriver = webdriver.Chrome('/Users/judeslater/Downloads/chromedriver')#driver = webdriver.Firefox()driver.get('https://magicseaweed.com/Nosara-Surf-Report/445/Tide/')WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="msw-js-tide-list"]')))all_days = driver.find_elements_by_xpath('//*[@id="msw-js-tide-list"]/div')ocean_data = all_days[1] # there is one hidden `div` which I have to skip to get today's dataprint(ocean_data.text)driver.quit()我可以使用[2]in做同样的事情xpath(因为python开始索引0但xpath开始于1)并在 word 中使用find_element_...without char只得到一个结果selementocean_data = driver.find_element_by_xpath('//*[@id="msw-js-tide-list"]/div[2]')print(ocean_data.text)from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC import timeimport datetimedriver = webdriver.Chrome('/Users/judeslater/Downloads/chromedriver')#driver = webdriver.Firefox()driver.get('https://magicseaweed.com/Nosara-Surf-Report/445/Tide/')WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="msw-js-tide-list"]')))ocean_data = driver.find_element_by_xpath('//*[@id="msw-js-tide-list"]/div[2]')print(ocean_data.text)driver.quit()

吃鸡游戏

for value in element:为什么不使用 element[0] 而不是这个 for 循环,因为您只需要该列表的第一个元素('msw-js-tide-list'),它给出了今天的日期。Ocean_Data = element[0]print(Ocean_Data.text)time.sleep(1)if contains_word(str(Ocean_Data), days[Break_Key]) == True:   break
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python