我想出了这个python代码,该代码应该带有包含数千个RSS url的文本文件(每行一个),并创建一个PER文件的PER文件。
这是我到目前为止所做的:
urls = open('file.txt','r').read().splitlines()
opml = open('opml.xml','w')
opml.write('<?xml version="1.0" encoding="UTF-8"?>\n<opml version="1.0">\n<head>\n<title>My Rss list</title>\n</head>\n<body>\n')
for i in xrange(500):
opml.write('<outline title="RSS Site %d" type="rss" xmlUrl = "%s"/>\n'%(i + 1, urls[i]))
opml.write('</body>\n</opml>')
opml.close()
但是它有问题,结果文件最终看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>My Rss list</title>
</head>
<body>
它缺少关闭体和关闭的opml,显然它缺少了轮廓位。
在python shell中,当我运行脚本时,我收到此错误:
Traceback (most recent call last): File "C:/Users/nzbit/Desktop/Convert To OPML Python Test/converttoopml.py", line 6, in <module> for i in xrange(500): NameError: name 'xrange' is not defined
更新:以下是生成的XML文件中的最新一期:
<outline title="RSS Site 1" type="rss" xmlUrl = "��tp://www.gardenleisurepools.com/forum/external?type=rss2"/>
<outline title="RSS Site 2" type="rss" xmlUrl = ""/>
<outline title="RSS Site 3" type="rss" xmlUrl = "http://www.meguiarsonline.com/forum/external.php?type=RSS2"/>
<outline title="RSS Site 4" type="rss" xmlUrl = ""/>
<outline title="RSS Site 5" type="rss" xmlUrl = "http://www.newportri.com/board/external?type=rss2"/>
<outline title="RSS Site 6" type="rss" xmlUrl = ""/>
<outline title="RSS Site 7" type="rss" xmlUrl = "http://www.abandonware-forums.org/forums/external?type=rss2"/>
<outline title="RSS Site 8" type="rss" xmlUrl = ""/>
<outline title="RSS Site 9" type="rss" xmlUrl = "https://www.lrcsite.com/forum/external.php?type=RSS2"/>
<outline title="RSS Site 10" type="rss" xmlUrl = ""/>
<outline title="RSS Site 11" type="rss" xmlUrl = "http://www.accutane-recall.com/forums/external?type=rss2"/>
答案 0 :(得分:0)
我相信你在python3而不是python2中运行你的代码,你必须在Python 3中将xrange()
更改为range()
:
for i in range(500):
更新:这基本上是基于您的代码的想法,它将创建多个文件,如opml1.xml,opml2.xml,opml3.xml
...并写入内容每500行。由于您的file.txt
使用UCS-2
进行编码,因为它是utf-16,您可以使用此encoding='utf-16'
打开文件,然后就不会有特殊字符。
with open('file.txt', encoding='utf-16') as f: #open with utf-16
urls = f.read().splitlines()
fileNum=1
for i in range(500,len(urls)+500,500): #loop with per 500
with open('opml'+str(fileNum)+'.xml','w') as opml:
opml.write('<?xml version="1.0" encoding="UTF-8"?>\n<opml version="1.0">\n<head>\n<title>My Rss list</title>\n</head>\n<body>\n')
if i >len(urls):
for j in range(i-500,len(urls)):
opml.write('<outline title="RSS Site %d" type="rss" xmlUrl = "%s"/>\n'%(j + 1, urls[j]))
else:
for j in range(i-500,i):
opml.write('<outline title="RSS Site %d" type="rss" xmlUrl = "%s"/>\n'%(j + 1, urls[j]))
opml.write('</body>\n</opml>')
opml.close()
fileNum+=1 #increase file number per loop
答案 1 :(得分:0)
这对我来说毫无问题:
urls = open('file.txt','r').read().splitlines()
opml = open('opml.xml','w')
opml.write('<?xml version="1.0" encoding="UTF-8"?>\n<opml version="1.0">\n<head>\n<title>My Rss list</title>\n</head>\n<body>\n')
for i in range(500):
if len(urls) > i:
opml.write('<outline title="RSS Site %d" type="rss" xmlUrl = "%s"/>\n'%(i + 1, urls[i]))
else:
break
opml.write('</body>\n</opml>')
opml.close()
以python3
运行文件