[英]How can I create new directory and save file using urllib2 in Python?
我正在尝试创建一个python脚本来检查主机是否还活着,如果是,请将网站下载到results /目录。 学习了该方法后,我将继续研究如何蜘蛛化并启动其他子进程(例如,在检查完成并加载保存的文件后启动nikto / skipfish)。
#! /usr/bin/python
import os
import sys
import urllib
import urllib2
import subprocess
# Where the magic happens
str1 = raw_input("Enter your target: ")
print "Target = ", str1
print "commencing testing on", str1
# Let's set the user-agent headers
http_headers = {"User-Agent":"Mozilla/5.0"}
request = urllib2.Request(str1)
response = urllib2.urlopen(request)
payload = response.read()
dir_path = os.path.join(self.results)
os.makedirs(dir_path)
**with open(os.join.path(dir_path, 'index.html', 'wb') as file:
file.write(payload)
print str1, "index written to file"**
# Send an email to notify us when complete
var = "world"
pipe = subprocess.Popen(["./email.sh", var], stdout=subprocess.PIPE)
result = pipe.stdout.read()
print result
我收到以下错误消息:
File "./webtest.py", line 43
with open(os.join.path(dir_path, 'index.html', 'wb') as file:
^
SyntaxError: invalid syntax
关闭括号后出现错误(来自Phil的回答):
Traceback (most recent call last):
File "./webtest.py", line 41, in <module>
dir_path = os.path.join(self.results)
NameError: name 'self' is not defined
您错过了一个括号:
with open(os.join.path(dir_path, 'index.html', 'wb')) as file:
编辑
该行与所需目录有关。 因为您不在课程中,所以出现错误(因此“ self”不存在)。 最好的措施是将其替换为“结果”并指定结果在哪里。 例如:
results = "/resultsdir/"
dir_path = os.path.join(results)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.