【问题标题】:mcx bhavcopy webscrape using python over multiple dates but no outputs and no errormcx bhavcopy webscrape 在多个日期使用 python 但没有输出也没有错误
【发布时间】:2021-08-18 16:35:57
【问题描述】:

下面只是没有给出任何输出。我正在尝试从下面指定的 xpath 下载 csv 文件,但程序只是运行不提供输出。请告知。

    import chromedriver_binary
    import os
    import requests
    import random
    import time
    import datetime 
    import csv
    import pandas as pd
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

    driver = webdriver.Chrome(ChromeDriverManager().install())  
    driver.get("https://www.mcxindia.com/market-data/bhavcopy")
    download_dir = "F:\\commodity research"
    print("good going!!")

    for d in pd.date_range('11/08/2021', '13/08/2021',periods = 1):
          s = driver.find_element_by_xpath("//*[@id='txtDate']")
          driver.execute_script("arguments[0].removeAttribute('readonly')", s)
          k = driver.find_element_by_id("txtDate")
          k.send_keys("d")
          b = driver.find_element_by_xpath("//*[@id='btnShowDatewise']").click()
          time.sleep(5)
          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//* 
          [@id='lnkExpToCSV']"))).click()
          #f = driver.find_element_by_xpath("//*[@id='lnkExpToCSV']").click()
          # time.sleep(3)

    driver.close()

【问题讨论】:

    标签: python asp.net selenium selenium-webdriver web-scraping


    【解决方案1】:

    问题是您需要为您的 chromedriver 设置选项并设置下载的默认目录。 您可以使用以下代码来做到这一点:

    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-sh-usage')
    prefs = {
        'download.default_directory': '/var/www/html/', # Replace /var/www/html with your path for downloads
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing_for_trusted_sources_enabled": False,
        "safebrowsing.enabled": False
    }
    options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    

    似乎对我有用的完整代码:

    import chromedriver_binary
    import os
    import requests
    import random
    import time
    import datetime
    import csv
    import pandas as pd
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-sh-usage')
    prefs = {
        'download.default_directory': '/var/www/html/', # Replace /var/www/html with your path for downloads
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing_for_trusted_sources_enabled": False,
        "safebrowsing.enabled": False
    }
    options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    driver.get("https://www.mcxindia.com/market-data/bhavcopy")
    download_dir = "F:\\commodity research"
    print("good going!!")
    for d in pd.date_range('11/08/2021', '13/08/2021',periods = 1):
            s = driver.find_element_by_xpath("//*[@id='txtDate']")
            driver.execute_script("arguments[0].removeAttribute('readonly')", s)
            k = driver.find_element_by_id("txtDate")
            k.send_keys("d")
            b = driver.find_element_by_xpath("//*[@id='btnShowDatewise']").click()
            time.sleep(5)
            WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//* [@id='lnkExpToCSV']"))).click()
            #f = driver.find_element_by_xpath("//*[@id='lnkExpToCSV']").click()
            time.sleep(10)
    driver.close()
    

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 2012-08-02
      • 2014-12-13
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多