【问题标题】:How to define configparser in python 3如何在 python 3 中定义 configparser
【发布时间】:2017-11-08 23:40:47
【问题描述】:

我正在开发一个项目,该项目将登录网站并评论用户生成的内容。

我使用 selenium、chrome 驱动程序和 python 3。所有凭据用户名、密码和 chromedriver 位置都在单独的 config.ini 文件中配置。

这是python脚本:

#!/usr/bin/python
import os
import time
import getpass
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from configparser import ConfigParser

# Reading configuration file
config = configparser.ConfigParser()
config.sections()

parser = ConfigParser()
parser.read('config.ini')
parameters = {}


for pairs in parser.sections():         # Parse the configuration file 
    for name, value in parser.items(pairs):
        parameters[name] = value

# Automating your browser 
chromedriver  = parameters["path"]
os.environ["webdriver.chrome.driver"] = chromedriver

#Uncomment this block if you don't want images to load(makes the procss a little bit faster)
'''
chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chromeOptions.add_experimental_option("prefs",prefs)
browser = webdriver.Chrome(chromedriver, chrome_options=chromeOptions)
'''

browser = webdriver.Chrome(chromedriver)
browser.set_window_size(1120, 550)
browser.get("http://www.website.com")       # website home page 
time.sleep(3)                               

# Logging into website
form = browser.find_element_by_class_name('regular_login')
email = form.find_element_by_name("email")
password = form.find_element_by_name("password")
email.send_keys(parameters["email_id"])
try:
    pass_word = parameters["pass_word"]
except:
    pass_word = getpass.getpass()               # If you want to enter password on terminal
password.send_keys(pass_word)
password.send_keys(Keys.RETURN)
time.sleep(2)                                   

# Fetching answers page of t6he user
answers_url = "https://www.website.com/" + parameters["username"] + "/answers"      
browser.get(answers_url)                                    

 #commenting answers one by one from top to bottom 
counter=0
while True:
    try:
        elem=browser.find_element_by_xpath("//*[@action_click='enter']")
        counter=counter+1
        elem.click()
        time.sleep(4)
    except:
        break

print (str(counter) +" answers commented..")

我一直在,

 config = configparser.ConfigParser()
NameError: name 'configparser' is not defined

请,任何人都可以回答我如何定义配置。

【问题讨论】:

  • 您收到错误是因为您尚未导入 configparser 您已导入 ConfigParser 来自 configparser。从错误所在的位置往下看 3 行,你会发现你没有使用过 configparser.Configparser()
  • 感谢@DavidG。我应该导入 configparser 还是将 config = configparser.ConfigParser() 替换为 config = ConfigParser.ConfigParser()

标签: python configuration


【解决方案1】:

出现此错误是因为您尚未实际导入configparser。您已经 configparser 导入了一些东西,但实际上并没有导入 configparser 本身。

对此有两种解决方案。

1) 您可以通过导入模块来定义它。 import configparser

2) 或者按照您在错误后 3 行中所做的操作并使用 config = ConfigParser()

如果您使用 configparser 的其他部分,那么我建议您导入整个模块并使用选项 1。如果您只使用 ConfigParser,那么我会使用选项 2。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多