在Python中卷曲并输出到单个文本文件 [英] Curling in Python and Outputting into Individual Text Files

查看:284
本文介绍了在Python中卷曲并输出到单个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从.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中卷曲并输出到单个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆