简体   繁体   English

Python 只写第一行

[英]Python only writing first line

I have a text file which I read in and then I extract the data I require and try sending it to a different new text file, but only the first line gets into the new text file.我有一个我读入的文本文件,然后我提取我需要的数据并尝试将它发送到另一个新的文本文件,但只有第一行进入新的文本文件。

import requests

url_file = open('url-test.txt','r')
out_file = open('url.NDJSON','w')

for url in url_file.readlines():
    html = requests.get(url).text

out_file.writelines(html)
out_file.close()

try:尝试:

for url in url_file.readlines():
    html = requests.get(url).text
    out_file.write(html)

or要么

lines = []
for url in url_file.readlines():
    html = requests.get(url).text
    # verify you are getting the expected data
    print(111111, html)
    lines.append(html)
out_file.writelines(lines)

either append the string in html or use the writelines statement in for loop要么 append html 中的字符串,要么在 for 循环中使用 writelines 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM