Selenium scraping:改变时区
Selenium scraping: changing timezone
我 运行 我的无头 (PhantomJS) 浏览器通过 Selenium 的网站有不同的时区,所以我得到了很多条目的错误日期。因此我抓取的结果显示错误 dates/times(我在美国东部时间,看起来网站默认是格林威治标准时间)。
我正在从 this website. You can get an idea of how i'm scraping dates through a previous question on SO 抓取。但是请注意,我目前并没有抓取游戏时间,所以我不想将其纳入解决方案。
同样的问题被问到here但是我不知道如何测试'obvious'查看网站默认时间的解决方案。我想有人会要求客户提供一个时间,而我现在的时间是 add/subtract 小时?如果有更好的方法,谁能告诉我该怎么做and/or。
编辑:我想要的是将网站抓取的数据从默认 (GMT) 更改为我的时间 (EST)。这将避免不得不增加时间;这些日期将反映它们对我来说是什么。
据我所知:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support.select import Select
driver = webdriver.PhantomJS(executable_path=r'C:/phantomjs.exe')
driver.get('http://www.oddsportal.com/hockey/usa/nhl/results/')
zoneDropDownID = "timezone-content"
driver.implicitly_wait(5)
zoneDropDownElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(zoneDropDownID))
# Select(zoneDropDownID).select_by_visible_text("Eastern") # strobject has no attribute
test = zoneDropDownID.select_by_visible_text("Eastern").click() # TimeOut exception - not found
driver.close()
但我无法点击它。我应该搜索 class 吗?
更好的测试方法是使用 chromedriver 或类似的东西。好处是,您可以直观地检查您的脚本在做什么。这是一个示例代码(没有错误处理),可以执行您想要的操作。
请注意,chromedriver.exe 必须与脚本位于同一位置。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--lang=en")
chrome = webdriver.Chrome(chrome_options=chrome_options)
wait = WebDriverWait(chrome, 300)
import time
chrome.get("http://www.oddsportal.com/hockey/usa/nhl/results/")
dropdown = wait.until(EC.presence_of_element_located((By.ID,"user-header-timezone-expander")))
dropdown.click()
userHeader = chrome.find_element_by_id('user-header-timezone')
time.sleep(2)
ahref = userHeader.find_elements_by_tag_name('a')
for a in ahref:
print(a.get_attribute("text"))
if "Eastern Time" in a.get_attribute('text'):
a.click()
time.sleep(10)
chrome.close()
就去那个url:
driver.get('http://www.oddsportal.com/set-timezone/15/')
我 运行 我的无头 (PhantomJS) 浏览器通过 Selenium 的网站有不同的时区,所以我得到了很多条目的错误日期。因此我抓取的结果显示错误 dates/times(我在美国东部时间,看起来网站默认是格林威治标准时间)。
我正在从 this website. You can get an idea of how i'm scraping dates through a previous question on SO
同样的问题被问到here但是我不知道如何测试'obvious'查看网站默认时间的解决方案。我想有人会要求客户提供一个时间,而我现在的时间是 add/subtract 小时?如果有更好的方法,谁能告诉我该怎么做and/or。
编辑:我想要的是将网站抓取的数据从默认 (GMT) 更改为我的时间 (EST)。这将避免不得不增加时间;这些日期将反映它们对我来说是什么。
据我所知:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support.select import Select
driver = webdriver.PhantomJS(executable_path=r'C:/phantomjs.exe')
driver.get('http://www.oddsportal.com/hockey/usa/nhl/results/')
zoneDropDownID = "timezone-content"
driver.implicitly_wait(5)
zoneDropDownElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(zoneDropDownID))
# Select(zoneDropDownID).select_by_visible_text("Eastern") # strobject has no attribute
test = zoneDropDownID.select_by_visible_text("Eastern").click() # TimeOut exception - not found
driver.close()
但我无法点击它。我应该搜索 class 吗?
更好的测试方法是使用 chromedriver 或类似的东西。好处是,您可以直观地检查您的脚本在做什么。这是一个示例代码(没有错误处理),可以执行您想要的操作。 请注意,chromedriver.exe 必须与脚本位于同一位置。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--lang=en")
chrome = webdriver.Chrome(chrome_options=chrome_options)
wait = WebDriverWait(chrome, 300)
import time
chrome.get("http://www.oddsportal.com/hockey/usa/nhl/results/")
dropdown = wait.until(EC.presence_of_element_located((By.ID,"user-header-timezone-expander")))
dropdown.click()
userHeader = chrome.find_element_by_id('user-header-timezone')
time.sleep(2)
ahref = userHeader.find_elements_by_tag_name('a')
for a in ahref:
print(a.get_attribute("text"))
if "Eastern Time" in a.get_attribute('text'):
a.click()
time.sleep(10)
chrome.close()
就去那个url:
driver.get('http://www.oddsportal.com/set-timezone/15/')