简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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