【问题标题】:Webscraping - Selenium - Python网页抓取 - Selenium - Python
【发布时间】:2019-06-18 18:10:00
【问题描述】:

我想提取所有参加过比赛的梦幻球队。要遍历日期,我只需更改 URL 的一小部分,如下面的代码所示:

#Packages:

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 pandas as pd


# Driver
chromedriver =("C:/Users/Michel/Desktop/python/package/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(chromedriver)

# Dataframe that will be use later 
results = pd.DataFrame()
best_lineups=pd.DataFrame()
opti_lineups=pd.DataFrame()

#For loop over all DATES:

calendar=[]
calendar.append("2019-01-10")
calendar.append("2019-01-11")

for d in calendar:
    driver.get("https://rotogrinders.com/resultsdb/date/"+d+"/sport/4/")

然后,要访问当天的不同比赛,您需要点击contest 标签。我使用以下代码找到并单击它。

 # Find "Contest" tab   
    contest= driver.find_element_by_xpath("//*[@id='root']/div/main/main/div[2]/div[3]/div/div/div[1]/div/div/div/div/div[3]")
    contest.click()

我只是检查并复制选项卡的 xpath。但是,大多数时候它都在工作,但有时我会收到一条错误消息“无法找到元素...”。此外,它似乎只适用于我日历循环中的第一个日期,并且在下一次迭代中总是失败......我不知道为什么。我尝试以不同的方式定位它,但我觉得我错过了以下内容:

contests=driver.find_element_by_xpath("//*[@role='tab']

一旦成功点击比赛选项卡,当天的所有比赛都会出现,您可以点击链接访问该比赛的所有参赛作品。我存储了比赛以便遍历所有内容,如下所示:

    list_links = driver.find_elements_by_tag_name('a')
    hlink=[]
    for ii in list_links:
        hlink.append(ii.get_attribute("href"))
    sub="https://rotogrinders.com/resultsdb"
    con= "contest"
    contest_list=[]
    for text in hlink:
        if sub in text:
            if con in text:
                contest_list.append(text)
# Iterate through all the entries(user) of a contest and extract the information of the team entered by the user 

    for c in contest_list:
        driver.get(c)

然后,我想提取参加比赛的所有参与者团队并将其存储在数据框中。我能够在比赛的第一页成功地做到这一点。

# Waits until tables are loaded and has text. Timeouts after 60 seconds
        while WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.XPATH, './/tbody//tr//td//span//a[text() != ""]'))):

# while ????: 

# Get tables to get the user names
            tables = pd.read_html(driver.page_source)
            users_df  = tables[0][['Rank','User']]
            users_df['User'] = users_df['User'].str.replace(' Member', '')

# Initialize results dataframe and iterate through users

            for i, row in users_df.iterrows():

                rank = row['Rank']
                user = row['User']

    # Find the user name and click on the name
                user_link = driver.find_elements(By.XPATH, "//a[text()='%s']" %(user))[0]
                user_link.click()

    # Get the lineup table after clicking on the user name
                tables = pd.read_html(driver.page_source)
                lineup = tables[1]

    #print (user)
    #print (lineup)

    # Restructure to put into resutls dataframe
                lineup.loc[9, 'Name'] = lineup.iloc[9]['Salary']
                lineup.loc[10, 'Name'] = lineup.iloc[9]['Pts']

                temp_df = pd.DataFrame(lineup['Name'].values.reshape(-1, 11), 
                columns=lineup['Pos'].iloc[:9].tolist() + ['Total_$', 'Total_Pts'] )

                temp_df.insert(loc=0, column = 'User', value = user)
                temp_df.insert(loc=0, column = 'Rank', value = rank)
                temp_df["Date"]=d
                results = results.append(temp_df)
            #next_button = driver.find_elements_by_xpath("//button[@type='button']")
            #next_button[2].click()



            results = results.reset_index(drop=True)



driver.close()

但是,还有其他页面,要访问它,您需要点击底部的小箭头next button。此外,您可以无限期地点击该按钮;即使没有更多条目。因此,我希望能够遍历所有包含条目的页面,并在没有更多条目时停止并更改比赛。我尝试实现一个while循环来做到这一点,但我的代码没有工作......

【问题讨论】:

    标签: python selenium web-scraping while-loop


    【解决方案1】:

    在对该页面执行任何操作之前,您必须确保该页面完全加载。

    此外,它似乎只适用于我日历循环中的第一个日期 并且总是在下一次迭代中失败

    通常当 selenium 加载浏览器页面时,它会尝试查找元素,即使它没有完全加载。我建议您重新检查您尝试单击的元素的 xpath

    还尝试查看页面何时完全加载并使用time.sleep(number of seconds) 以确保您点击了该元素,或者您可以检查某个特定元素或元素的属性,以告知您页面已加载。

    还有一个建议是,您可以使用driver.current_url 查看您的目标网页。我在处理多个选项卡时遇到了这个问题,我不得不告诉 python/selenium 手动切换到该选项卡

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2013-11-13
      • 2020-11-23
      • 2019-06-30
      相关资源
      最近更新 更多