来自 url 的数据在列表中打开

data from url open in list

我想从网上下载一个文件,即:http://www.malwaredomainlist.com/hostslist/ip.txt 并将其放入列表中以进一步操作列表中的项目。

我试过了

print "Downloading with urllib2"
    f = urllib2.urlopen(malwareurl)
    data = [f.read()]
    result = [stuff.replace("\r\n", "/32,") for stuff in data]
    print result
    print len([result])

列表本身"looks"很好:

['100.42.50.110/32,103.14.120.121/32,.......']

但长度只有1
我想我需要遍历阅读行并在列表中为每个阅读行创建项目,对吗?

我认为你过于复杂了:

print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read().split("\r\n")

# If you want to add '/32' to each IP
ips = [x + "/32" for x in ips if x]
>>> r=urllib2.urlopen('http://www.malwaredomainlist.com/hostslist/ip.txt')
>>> f=open('e.txt','w')
>>> ff=r.read()
>>> f.write(ff)
>>> f.close()
list1 = []
print "Downloading with urllib2"
f = urllib2.urlopen(malwareurl)
ips = f.read()
for ip in ips:
   ip = ip.strip()
   ip = ip + "/32"
   list1.append(ip)
print list1
print len(list1)