Using selenium/flask to scrape a website and populate a web page based on that info. Results keeping disappearing

I’m trying to make a Fantasy Football Optimizer. Since ESPN doesn’t have an API and requires a login to access the draft room, I’m using Selenium. My goal is to have a website with a launch button that, when clicked, creates a driver and opens a new window. Once the user navigates to their draft room, they hit a connect button, which should scrape the required information in that window and display it on the site. The problem is that when the user clicks connect, the correct information appears on my site for a split second but then disappears. Even the console log resets. However, if I save the dataframe created by the scraper and upload it directly to the backend, it works perfectly. What can cause this and how do I fix it?

@app.route('/launch-ESPN', methods=['POST'])
def launch_ESPN():
    global manager
    data = request.json
    browser = data.get('browser')
    manager = scraper.ESPNManager()
    return manager.launch_ESPN(browser)
    
@app.route('/scrape-ESPN', methods=['POST'])
def scrape_ESPN():
    global manager
    if manager:
        return manager.scrape_ESPN()
    else:
        return jsonify({"error": "Manager not initialized"}), 400
class ESPNManager:
    def __init__(self):
        self.driver = None

    def launch_ESPN(self, browser):
        if browser == 'chrome':
            self.driver = webdriver.Chrome()
        elif browser == 'firefox':
            self.driver = webdriver.Firefox()
        else:
            return 'Please select a valid browser'
        
        self.driver.get('https://www.espn.com/fantasy/football/')
        return 'ESPN launched successfully'

    def scrape_ESPN(self):
        dropdown_element = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/section/div/div[2]/main/div/div/div[3]/div[1]/div[1]/div[2]/div[1]/div/select')

        # Create a Select object
        dropdown = Select(dropdown_element)
        teams = dropdown.options
        table = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/section/div/div[2]/main/div/div/div[3]/div[1]/div[1]/div[2]/div[2]/div/div/div/div/div[2]/table")
        roster_size = len(table.find_elements(By.TAG_NAME, "tr")) - 1
        # roster is df where column names are team names and rows are roster requirements
        positions = []
        uniq_num = 0
        roster = pd.DataFrame()
        for i in range(len(teams)):
            dropdown.select_by_index(i)
            team_df = pd.DataFrame(columns=["Player", "Position"])
            for j in range(1, roster_size + 1):
                team_ele = self.driver.find_element(By.XPATH, f"/html/body/div[1]/div[1]/section/div/div[2]/main/div/div/div[3]/div[1]/div[1]/div[2]/div[2]/div/div/div/div/div[2]/table/tbody/tr[{j}]")
                # get the second time it says ("tag name","td")
                try:
                    element = self.driver.find_elements(By.CSS_SELECTOR, ".jsx-2810852873.table--cell.player-column")[j]
                    player = element.get_attribute("title")
                    if player is None or player == "":
                        player = " "
                except:
                    player = " "
                position_ele = team_ele.find_element(By.TAG_NAME, "td")
                team_df.loc[j] = [player, position_ele.text]
                if i == 0:
                    positions.append(position_ele.text + str(uniq_num))
                    uniq_num += 1
            roster[teams[i].text] = team_df["Player"]
        roster.index = positions
        roster.to_csv('roster.csv')
        return roster.to_json()

As shown above, I’m saving the dataframe to roster.csv. If the function just uploads that csv and returns it, it works.

New contributor

Mark Webb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật