如何在Python中使用urllib2创建新目录并保存文件?

时间:2013-01-29 18:00:03

标签: python urllib2

我正在尝试创建一个python脚本来检查主机是否还活着,如果是,请将网站下载到结果/目录中。一旦我学会了如何做到这一点,我将分析出如何进行蜘蛛并启动其他子进程(例如在检查完成并启动已保存的文件后启动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

1 个答案:

答案 0 :(得分:1)

你错过了一个括号:

with open(os.join.path(dir_path, 'index.html', 'wb')) as file:

修改

该行与您想要的目录有关。这是错误的,因为你不在课堂上(所以“自我”不存在)。最好的做法是用“结果”替换它并指定结果的位置。例如:

results = "/resultsdir/"
dir_path = os.path.join(results)