首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Chromedriver在抓取时不断改变时区

Chromedriver在抓取时不断改变时区
EN

Stack Overflow用户
提问于 2019-03-04 14:23:36
回答 1查看 1.3K关注 0票数 0

下面是我的Python代码的开头,它成功地从这个website中抓取了所有表信息,并将其导出到一个CSV文件中。我唯一的问题是Chromedriver一直在改变右上角的时区,这最终会扭曲我的输出,因为它给一些游戏分配了错误的日期。我试着在页面源代码中查找链接或标签,这些链接或标签可以让我点击"GMT-8太平洋时区“,但不幸的是我什么也没找到。令人沮丧的是,当我将URL复制并粘贴到浏览器中时,Chrome会立即切换回太平洋时区。有人知道在使用Chromedriver抓取数据时如何解决这个时区问题吗?提前感谢!

代码语言:javascript
复制
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')
EN

回答 1

Stack Overflow用户

发布于 2019-03-04 14:47:56

看起来您可以使用以下命令进行设置:

代码语言:javascript
复制
driver.get("https://www.oddsportal.com/set-timezone/6/")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54977646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档