下面是我的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')
发布于 2019-03-04 14:47:56
看起来您可以使用以下命令进行设置:
driver.get("https://www.oddsportal.com/set-timezone/6/")
https://stackoverflow.com/questions/54977646
复制相似问题