目的是搜索一个 infile (html) 并在一个可以传递给 wget 的 outfile 中重现任何图像的 URL。这将是我用 Python 编写的第一个有用的东西,它似乎在 Fedora 上运行良好。我在任何地方都找不到像这样的东西。有没有人有改进的建议?
import fileinput
import re
#replace 'output.txt' with the name of your outfile
file = open('output.txt', 'w')
#prefix and postfix are how we discriminate your substring from the infile's line
prefix = '<img src='
postfix = '.jpg'
#read through the infile line-by-line
for line in fileinput.input():
if re.search(prefix, line):
#from if above, if you find the prefix, assign the integer to first_index
first_index = line.index(prefix)
if re.search(postfix, line):
#same as comment above, but for postfix
second_index = line.index(postfix)
#write your string plus an newline to the outfile
file.write(line[first_index+prefix.__len__():second_index+postfix.__len__()]+'\n')