Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在新的行上用python将文本写入txt文件?_Python_File - Fatal编程技术网

在新的行上用python将文本写入txt文件?

在新的行上用python将文本写入txt文件?,python,file,Python,File,因此,我试图检查url是否存在,如果存在,我想使用python将url写入文件。我还希望每个url都位于文件中自己的行上。以下是我已有的代码: import urllib2 在桌面上创建一个空白的TXT文件 urlhere = "http://www.google.com" print "for url: " + urlhere + ":" try: fileHandle = urllib2.urlopen(urlhere) data = fileHandle

因此,我试图检查url是否存在,如果存在,我想使用python将url写入文件。我还希望每个url都位于文件中自己的行上。以下是我已有的代码:

import urllib2  
在桌面上创建一个空白的TXT文件

urlhere = "http://www.google.com"   
print "for url: " + urlhere + ":"  

try: 
    fileHandle = urllib2.urlopen(urlhere)
    data = fileHandle.read()
    fileHandle.close()
    print "It exists"
然后,如果URL确实存在,则在文本文件中的新行上写入URL

except urllib2.URLError, e:
    print 'PAGE 404: It Doesnt Exist', e
如果URL不存在,请不要将任何内容写入该文件


像这样的东西怎么样:

import urllib2

url  = 'http://www.google.com'
data = ''

try:
    data = urllib2.urlopen(url).read()
except urllib2.URLError, e:
    data = 'PAGE 404: It Doesnt Exist ' + e

with open('outfile.txt', 'w') as out_file:
   out_file.write(data)

您的问题措辞有点混乱,但如果我理解正确,您所要做的就是使用urllib2测试url是否有效,以及是否将url写入文件?如果这是正确的,则以下各项应起作用

import urllib2
f = open("url_file.txt","a+")
urlhere = "http://www.google.com"   
print "for url: " + urlhere + ":"  

try: 
    fileHandle = urllib2.urlopen(urlhere)
    data = fileHandle.read()
    fileHandle.close()
    f.write(urlhere + "\n")
    f.close()
    print "It exists"

except urllib2.URLError, e:
    print 'PAGE 404: It Doesnt Exist', e
如果要测试多个URL但不想编辑python脚本,可以通过键入
python\u script.py使用以下脚本http://url_here.com“
。这可以通过使用sys模块实现,其中sys.argv[1]等于传递给python_script.py的第一个参数。在本例中,它是url(“”)

或者,如果您真的想让您的工作变得简单,您可以通过在命令行
python\u脚本中键入以下内容来使用以下脚本http://url1.com,http://url2.com
其中所有要测试的URL都用逗号分隔,不带空格

import urllib2,sys
f = open("url_file.txt","a+")
urlhere_list = sys.argv[1].split(",")   

for urls in urlhere_list:
    print "for url: " + urls + ":" 
    try: 
        fileHandle = urllib2.urlopen(urls)
        data = fileHandle.read()
        fileHandle.close()
        f.write(urls+ "\n")

        print "It exists"

    except urllib2.URLError, e:
        print 'PAGE 404: It Doesnt Exist', e
    except:
        print "invalid url"
f.close()
sys.argv[1].split()
如果不想使用命令行功能,也可以用脚本中的python列表替换。希望这对你有用,祝你的程序好运

注意 使用命令行输入的脚本在ubuntu linux上进行了测试,因此,如果您使用的是windows或其他操作系统,我不能保证它能够按照给出的指令工作,但它应该使用。

使用:


所以问题又来了?我希望能够输入“urlhere”的任何url,让程序测试它是否存在或是否是404页面,然后只将我尝试存在的url写入桌面上的txt文件。目前,我已将url检查到位,我所需要做的就是将url写入txt文件。这不是问题。好的,我如何使用python编辑文本文件?嘿,伙计!关闭,但这会将大量随机内容写入outfile.txt。我只想写一个url,它可以工作到outfile.txt。每次我运行这个程序时,它不是重新创建输出文件,而是从下一行开始修改现有文件。它崩溃了!谢谢你,伙计!这真的很有帮助!我很感激你为底部2付出的额外努力!!别担心!很高兴我能帮助你!这也给了我一些非常渴望的练习,所以我应该部分地感谢你。
import urllib2,sys
f = open("url_file.txt","a+")
urlhere_list = sys.argv[1].split(",")   

for urls in urlhere_list:
    print "for url: " + urls + ":" 
    try: 
        fileHandle = urllib2.urlopen(urls)
        data = fileHandle.read()
        fileHandle.close()
        f.write(urls+ "\n")

        print "It exists"

    except urllib2.URLError, e:
        print 'PAGE 404: It Doesnt Exist', e
    except:
        print "invalid url"
f.close()
import requests

def url_checker(urls):
    with open('somefile.txt', 'a') as f:
       for url in urls:
           r = requests.get(url)
           if r.status_code == 200:
              f.write('{0}\n'.format(url))

url_checker(['http://www.google.com','http://example.com'])