下面是我的Python代码的开头,它成功地从此website中删除了所有表信息并将其导出到CSV文件。我对这个刮刀唯一的问题是Chromedriver不断更改右上角的时区,最终通过为一些游戏分配错误的日期来扭曲我的输出。我已经尝试在页面源中查找链接或标签,这样我就可以点击“GMT-8太平洋时区”,但遗憾的是我找不到任何东西。令人沮丧的是,当我将URL复制并粘贴到我的浏览器中时,Chrome会立即切换回太平洋时区。使用Chromedriver刮取数据时有谁知道如何解决这个时区问题?提前致谢!
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
import pandas as pd
# set scope and create empty lists
year = 2018
lastpage = 50
Date = []
Time = []
Team1 = []
Team2 = []
Score = []
All_ML = []
Team1_ML = []
Team2_ML = []
driver = webdriver.Chrome()
driver.get('http://www.oddsportal.com/')
driver.execute_script('op.selectTimeZone(6);')
# set up for loop to loop through all pages
for x in range(1, lastpage + 1):
url = "http://www.oddsportal.com/baseball/usa/mlb-" + str(year) + "/results/#/page/" + str(x) + "/'"
driver.get(url)
# wait until java table loads and then grab data
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '//*[@id="tournamentTable"]')))
odds = element.text
print (odds)
# close temporary chrome screen
driver.close()
# reformat resulting text for consistency
odds = re.sub("[0-9] - ", str(year)[-1] + " -- ", odds)
odds = re.sub(" - ", "\nteam2", odds)
# split text by line
odds = odds.split("\n")
counter = 1
# set up loop to classify each line of text
for line in odds:
# if a game was abandoned or cancelled, set score to N/A
if re.match(".*( {1})[a-zA-Z]*\.$", line):
Score.append("N/A")
# if date format is matched, add to date list and reset counter
if re.match("(.{2} .{3} .{4}.*)", line):
currdate = line[:11]
Date.append(currdate)
counter = 1
# if time format is matched at beginning of string, add time to list, add team1 to list, check if there was a new date for this game. if not, add current date from previous game
elif re.match('(.{2}:.{2})', line):
Time.append(line[:5])
Team1.append(line[6:])
if counter > 1:
Date.append(currdate)
counter += 1
# if its a team2 line, add to team2 list. if score is on the same line, add to score list
elif re.match("team2.*", line):
if re.match(".*:.*", line):
Score.append(re.sub("[a-zA-Z]* *", "", line[-5:]))
Team2.append(re.sub(" {1}[0-9]*:[0-9]*", "", line[5:len(line)]))
else:
Team2.append(re.sub(" {1}[a-zA-Z]*\.", "", line[5:]))
# if score is on it's own line, add to score list
elif re.match(".*:.*", line):
Score.append(re.sub(" ", "", line))
# add all moneylines to a list
elif re.match("[+\-.*]", line):
All_ML.append(line)
# add odd money lines to list1, even moneylines to list 2
Team1_ML = All_ML[0::2]
Team2_ML = All_ML[1::2]
# create dataframe with all lists
df = pd.DataFrame(
{'Date': Date,
'Time': Time,
'Team1': Team1,
'Team2': Team2,
'Score': Score,
'Team1_ML': Team1_ML,
'Team2_ML': Team2_ML})
# save
df.to_csv('odds2018.csv')
为了充实pguardiario的注释,如果你使用Chrome devtools来查看右上角的按钮,每个按钮都会触发到https://www.oddsportal.com/set-timezone/n/
的链接,其中n
是一些时区代码。这些功能实际上实际上触发了一个功能op.selectTimeZone(n)
,它会改变你在屏幕上的时区。您可以通过输入op.selectTimeZone(n)
在Chrome控制台中对此进行试验。
如果这适合您,您可以通过使用模拟控制台javascript调用来合并它,其中n
是所选时区的代码:
driver.execute_script('op.selectTimeZone(n);')
您可以在每次驱动程序初始化调用后添加,强制设置时区,例如:
for x in range(1, lastpage + 1):
url = "http://www.oddsportal.com/baseball/usa/mlb-" + str(year) + "/results/#/page/" + str(x) + "/'"
driver = webdriver.Chrome()
driver.get(url)
# Set timezone
driver.execute_script('op.selectTimeZone(6);')
# wait until java table loads and then grab data
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '//*[@id="tournamentTable"]')))
odds = element.text
请注意,您可能需要设置等待计时器,因为您在选择时区后添加了额外的执行。
此外,除非您计划并行化for
循环,否则您无需为每个循环重置驱动程序调用。如果您进行驱动程序初始化并关闭循环,这可能会运行得更快。
编辑:
因此,如果您直接访问结果页面,您将无法在不触发页面重新加载的情况下设置时区。您可能需要进行设置并加载循环,例如
driver = webdriver.Chrome()
driver.get('http://www.oddsportal.com/')
# Proc JS on-click for timezone selection button
driver.execute_script("op.showHideTimeZone();ElementSelect.expand( 'user-header-timezone' , 'user-header-timezone-expander' , null , function(){op.hideTimeZone()} );this.blur();")
driver.execute_script('op.selectTimeZone(6);')
for x in range(1, lastpage + 1):
url = "http://www.oddsportal.com/baseball/usa/mlb-" + str(year) + "/results/#/page/" + str(x) + "/'"
driver.get(url)
# wait until java table loads and then grab data
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '//*[@id="tournamentTable"]')))
odds = element.text
print(odds)
# close temporary chrome screen
driver.close()
看起来你可以设置:
driver.get("https://www.oddsportal.com/set-timezone/6/")