【发布时间】:2018-01-26 14:59:24
【问题描述】:
我对python很陌生。我正面临“wget”和“urllib.urlretrieve(str(myurl),tail)”的问题
当我运行脚本时,它正在下载文件,但文件名以“?”结尾
我的完整代码:
import os
import wget
import urllib
import subprocess
with open('/var/log/na/na.access.log') as infile, open('/tmp/reddy_log.txt', 'w') as outfile:
results = set()
for line in infile:
if ' 200 ' in line:
tokens = line.split()
results.add(tokens[6]) # 7th token
for result in sorted(results):
print >>outfile, result
with open ('/tmp/reddy_log.txt') as infile:
results = set()
for line in infile:
head, tail = os.path.split(line)
print tail
myurl = "http://data.xyz.com" + str(line)
print myurl
wget.download(str(myurl))
# urllib.urlretrieve(str(myurl),tail)
输出:
# python last.py
0011400026_recap.xml
http://data.na.com/feeds/mobile/android/v2.0/video/games/high/0011400026_recap.xml
latest_1.xml
http://data.na.com/feeds/mobile/iphone/article/league/news/latest_1.xml
currenttime.js
列出文件:
# ls
0011400026_recap.xml? currenttime.js? latest_1.xml? today.xml?
【问题讨论】:
-
看起来像换行符,因为它每次都打印出额外的行。没有看到
line就很难确定@ -
@CoryMadden 我还应该提供哪些信息?
-
line对于初学者。 -
myurl = 'data.na.com' + str(line) print myurl # wgproc = subprocess.Popen(['wget', '-r', '--tries=10', 'str( url)', '-o', 'log'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # (standardout, junk) = wgproc.communicate() wget.download(str(myurl)) #urllib. urlretrieve(str(myurl),tail)
-
你显示的代码不可能给出你显示的输出。此外,缩进是错误的。更不用说在 cmets 中发布代码了。也不需要临时文件。整条线上
200上的匹配迟早会导致误匹配。也就是说,我的水晶球告诉我myurl = "http://data.xyz.com" + str(line.strip())确实是你想要的。