编写my webcrawler(Python)时获取形式参数名称时出现预期错误
我目前正在使用一个预构建的shell测试一个web爬虫程序,在这个过程中,我有点陷入了这个部分,在def编写my webcrawler(Python)时获取形式参数名称时出现预期错误,python,parameters,pycharm,Python,Parameters,Pycharm,我目前正在使用一个预构建的shell测试一个web爬虫程序,在这个过程中,我有点陷入了这个部分,在defcreate_data_files部分,它抱怨“需要正式的参数名” 此处引用的是迄今为止的部分代码(我使用的是PyCharm社区版2017.1.1) 您需要更改def create_data_文件(test,'www.startap.com/'):行,类似def create_data_文件(test,url='www.startap.com/'):您不能定义这样的函数,参数应该是名称,而不是
create_data_files
部分,它抱怨“需要正式的参数名”
此处引用的是迄今为止的部分代码(我使用的是PyCharm社区版2017.1.1)
您需要更改
def create_data_文件(test,'www.startap.com/'):
行,类似def create_data_文件(test,url='www.startap.com/'):
您不能定义这样的函数,参数应该是名称,而不是值。定义:def create_data_文件(test,url)
call:def create_data_files(“sss”,“www.sss.ss”)
遗憾的是,即使这样,它仍然显示错误。为了方便起见,制作了一个屏幕截图:对不起,我犯了一个错误,您应该编写def create_data_files(测试,url)
定义函数时,此处test
和url
是参数,然后可以在其他地方创建数据文件(“arg1”、“arg2”)
,此处传递的值是参数。参数和参数之间的差异。
# Each website is a separate project (folder)
def create_project_dir(directory):
if not os.path.exists(directory):
print('Creating directory ' + directory)
os.makedirs(directory)
create_project_dir('test')
# Create queue and crawled files (if not created)
def create_data_files(test,'www.startlap.com/'):
queue = os.path.join(project_name , 'queue.txt')
crawled = os.path.join(project_name,"crawled.txt")
if not os.path.isfile(queue):
write_file(queue, base_url)
if not os.path.isfile(crawled):
write_file(crawled, '')
# Create a new file
def write_file(path, data):
with open(path, 'w') as f:
f.write(data)
# Add data onto an existing file
def append_to_file(path, data):
with open(path, 'a') as file:
file.write(data + '\n')
# Delete the contents of a file
def delete_file_contents(path):
open(path, 'w').close()
# Read a file and convert each line to set items
def file_to_set(file_name):
results = set()
with open(file_name, 'rt') as f:
for line in f:
results.add(line.replace('\n', ''))
return results
# Iterate through a set, each item will be a line in a file
def set_to_file(links, file_name):
with open(file_name,"w") as f:
for l in sorted(links):
f.write(l+"\n")