如何从.txt文件中读取包含3个URL的多行,curl它们以解压HTML,并将它们全部输出到python中的各自的文件中? 我试过了 -
How do you read multiple lines containing say 3 URLs from a .txt file, "curl" them to extract the HTML, and output them all to their own individual files in python? I've tried-
import commands import os import json # RAW DATA input = open('uri.txt', 'r') t_1 = open('command', 'w') counter_1 = 0 for line in input: counter_1 += 1 if counter_1 < 3: filename = str(counter_1) print str(line) filename= str(count) command ='curl ' + '"' + str(line).rstrip('\n') + '"'+ '> ./rawData/' + filename output_1 = commands.getoutput(command) input.close()当我在PyCharm中运行我的代码时,它在我的目录中创建一个名为filename.txt的空白文件
When I run my code in PyCharm, it creates a single blank file in my directory called "filename.txt"
推荐答案您应该使用请求而不是从python中的cURL,因为这将使处理响应更容易。此外,使用文件的规范方法是使用上下文管理器: with open(filename)as f 。
You should use requests instead of cURL from within python, as this will make handling the responses much easier. Also, the canonical way to work with files is to use the context manager: with open(filename) as f.
更多推荐
在Python中卷曲并输出到单个文本文件
发布评论