简体   繁体   English

python中如何定义configparser 3

[英]How to define configparser in python 3

I'm working on a project that will log into the site and will comment the user generated content.我正在开发一个将登录该站点并评论用户生成的内容的项目。

I use selenium, chrome driver and python 3. All credentials user name, password and chromedriver location is configured in a separate config.ini file.我使用 selenium、chrome 驱动程序和 python 3. 所有凭据用户名、密码和 chromedriver 位置都在单独的 config.ini 文件中配置。

Here's the python script:这是 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..")

I keep getting,我不断得到,

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

Please, can anyone answer how can I define the configure.拜托,任何人都可以回答我如何定义配置。

This error occurs because you haven't actually imported configparser . 发生此错误是因为您尚未实际导入configparser You have imported something from configparser but haven't actually imported configparser itself. 已从 configparser导入了某些内容但实际上尚未导入configparser本身。

There are two solutions for this. 有两种解决方案。

1) You can either define it by importing the module. 1)您可以通过导入模块来定义它。 import configparser

2) Or do as you have done 3 lines down from the error and use config = ConfigParser() 2)或者按照从错误开始向下3行的方式进行操作,并使用config = ConfigParser()

If you are using other parts of configparser then I would recommend to import the whole module and go with option 1. If you're only using ConfigParser then I would go with option 2. 如果您正在使用configparser其他部分,那么我建议您导入整个模块并使用选项1。如果您仅使用ConfigParser那么我将使用选项2。

You Have config = configparser.ConfigParser() you should have config = ConfigParser()你有config = configparser.ConfigParser()你应该有config = ConfigParser()

You do not need to specify configparser because you are importing from it, so you just use the class you are importing which is ConfigParser .您不需要指定 configparser 因为您是从它导入的,所以您只需使用您正在导入的 class 即ConfigParser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM