【问题标题】:Web Scraping Using Selenium to get date based dataWeb Scraping 使用 Selenium 获取基于日期的数据
【发布时间】:2020-08-22 08:10:49
【问题描述】:

我对 Web Scraping 非常陌生,我非常强调新。 我需要从网站上的表格中抓取数据。该表每天都在变化(股票价格)。到目前为止,我的代码只提取一天的数据,但我需要一次提取多天的数据。该网页有一个日历,您可以选择一天并显示其历史记录。 我正在使用硒。 这是我的部分代码,向您展示我在做什么`


from selenium import webdriver
from selenium.webdriver.chrome.options import Options



chrome_path = "C:\Program Files (x86)\chromedriver.exe"
chrome_options = Options()
chrome_options.add_argument("headless") 
driver = webdriver.Chrome(chrome_path , options = chrome_options , keep_alive = False) 
driver.get("http://www.casablanca-bourse.com/bourseweb/indice-ponderation.aspx?Cat=22&IdLink=298")


codelist = []
instrumentList = []
NbreList = []
CoursList = []
FacteurList = []
FacteurPlafList = []
Capitalist = []
poidList = []

for i in range(4,77):
    codepath = f"""//*[@id="Ponderation1_UpdatePanel1"]/table/tbody/tr[5]/td/table/tbody/tr[4]/td[2]"""
    code = driver.find_element_by_xpath(codepath)
    codelist.append(code.text)

【问题讨论】:

  • 我认为您的上述代码即使在一个日期内也无法抓取数据。根据您上面的列表 -codelist 将不包含 Code Isin 列 73 次(因为您正在循环 73 次)的第一个值,即 MA0000011488 默认日期 18/08/20
  • 这只是代码的一部分。该列表还有其他项目,并且运行良好。我压缩了列表中的元素
  • 可能是我忘记把{i}加到tr

标签: python selenium web-scraping


【解决方案1】:

更改日期并点击按钮

driver.find_element_by_id("Ponderation1_DateTimeControl1_TBCalendar").clear()
driver.find_element_by_id("Ponderation1_DateTimeControl1_TBCalendar").send_keys("19/08/2020")
driver.find_element_by_id("Ponderation1_ImageButton1").click()

for i in range(4,77):
    codepath = f"""//*[@id="Ponderation1_UpdatePanel1"]/table/tbody/tr[5]/td/table/tbody/tr[{i}]/td[2]"""
    code = driver.find_element_by_xpath(codepath)
    codelist.append(code.text)
    
print(codelist)

还把{i} 放在 tr 上

【讨论】:

    【解决方案2】:

    正如您为每一列定义了一个列表。我假设您想将每列的数据存储在单独的列表中,并且您想根据日期加载表。您可以定义下面的函数,然后调用您的函数来获取每一列的数据。

    def scraping_table (date, columnNumber):
        colList =[]
        colXpath = "//tr[td[text()='Code Isin']]//following-sibling::tr//td["+str(columnNumber)+"]"
    
        #Enter date in date picker
        datePicker = driver.find_element_by_name("Ponderation1$DateTimeControl1$TBCalendar")
        datePicker.clear()
        datePicker.send_keys(date)
        driver.find_element_by_name("Ponderation1$ImageButton1").click()
        time.sleep(6) #Wait for table to laod
        data = driver.find_elements_by_xpath(colXpath)
        if len(data) >2: # If table is empty for a date no record will be returned
            for i in range (2, len(data)-1):
                colList.append(data[i].text)
        return colList
    
    
    
    chrome_path = '..\drivers\chromedriver'
    chrome_options = Options()
    chrome_options.add_argument("headless")
    driver = webdriver.Chrome(chrome_path , options = chrome_options , keep_alive = False)
    driver.get("http://www.casablanca-bourse.com/bourseweb/indice-ponderation.aspx?Cat=22&IdLink=298")
    
    # Call function Now, Can pass date and column as per your need
    
    codelist = scraping_table('17/08/2020', 2) # Note your table has hidden columns and Code Isin is column number 2
    instrumentList = scraping_table('17/08/2020', 3)
    NbreList = scraping_table('17/08/2020', 4)
    CoursList = scraping_table('17/08/2020', 5)
    FacteurList = scraping_table('17/08/2020', 6)
    FacteurPlafList = scraping_table('17/08/2020', 7)
    Capitalist = scraping_table('17/08/2020', 8)
    poidList = scraping_table('17/08/2020', 9)
    
    # To illustrate i have printed values of 'Nombre de titres' column
    
    for num in NbreList:
        print(num)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 2021-01-05
      • 1970-01-01
      • 2019-01-31
      • 2023-01-09
      • 1970-01-01
      相关资源
      最近更新 更多