簡體   English   中英

用python在新行上將文本寫入txt文件?

[英]Writing text to txt file in python on new lines?

因此,我試圖檢查網址是否存在,如果存在,我想使用python將網址寫入文件。 我還希望每個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 python_script.py "http://url_here.com"使用以下腳本。 這可以通過使用sys模塊來實現,其中sys.argv [1]等於傳遞給python_script.py的第一個參數。 在此示例中,它是url(' http://url_here.com ')。

import urllib2,sys
f = open("url_file.txt","a+")
urlhere = sys.argv[1]   
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

或者,如果您真的想python python_script http://url1.com,http://url2.com工作,可以在命令行python python_script http://url1.com,http://url2.com中鍵入以下內容,以使用以下腳本python python_script 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()

如果不想使用命令行功能,也可以在腳本中用python列表替換sys.argv[1].split() 希望這對您有所幫助,並祝您程序順利。

note注意使用命令行輸入的腳本已在ubuntu linux上進行了測試,因此,如果您使用的是Windows或其他操作系統,我不能保證它會與給定的指令一起使用,但是應該可以。

使用requests

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'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM