在Python中卷曲并输出到单个文本文件

编程入门 行业动态 更新时间:2024-10-12 01:26:22
本文介绍了在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中卷曲并输出到单个文本文件

本文发布于:2023-11-24 05:56:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:卷曲   文本文件   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!