简体   繁体   English

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

[英]How can I create new directory and save file using urllib2 in Python?

I'm trying to create a python script to check if a host is alive, if so, download the website into a results/ directory. 我正在尝试创建一个python脚本来检查主机是否还活着,如果是,请将网站下载到results /目录。 Once I learn how to do this I will branch out on figuring out how to spider and launch other subprocesses (such as launching nikto/skipfish after checking is complete and loading the saved file). 学习了该方法后,我将继续研究如何蜘蛛化并启动其他子进程(例如,在检查完成并加载保存的文件后启动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

I receive the following error message: 我收到以下错误消息:

File "./webtest.py", line 43
    with open(os.join.path(dir_path, 'index.html', 'wb') as file:
                                                          ^
SyntaxError: invalid syntax

Error after closing the parenthesis (from Phil's Answer): 关闭括号后出现错误(来自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

You missed a parentheses: 您错过了一个括号:

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

EDIT 编辑

That line has to do with the directory that you want. 该行与所需目录有关。 It's giving errors because you're not in a class (so "self" doesn't exist). 因为您不在课程中,所以出现错误(因此“ self”不存在)。 The best course of action would be to replace it with just "results" and specify where results are. 最好的措施是将其替换为“结果”并指定结果在哪里。 For example: 例如:

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

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

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