PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 13, 2022

[FIXED] Why is the file saved showing None while console shows the correct response?

 August 13, 2022     concurrent.futures, output, python, python-requests, text     No comments   

Issue

When I try to save the responses to a file, the actual response is not saved even though it shows in the console. The result that is saved in the file is None. See examples below

from concurrent.futures import ThreadPoolExecutor
import requests
#from timer import timer


#########  create test file

URLsTest = '''
https://en.wikipedia.org/wiki/NBA
https://en.wikipedia.org/wiki/NFL
'''.strip()

with open('input.txt', 'w') as f:
    f.write(URLsTest)
    
####################

with open('input.txt', 'r') as f:
    urls=f.read().split('\n')    # url list

def fetch(tt):  # received tuple
    session, url = tt
    print('Processing')
    with session.get(url) as response:
        print(response.text)

#@timer(1, 5)
def main():
    with ThreadPoolExecutor(max_workers=100) as executor:
        with requests.Session() as session:  # for now, just one session
            results = executor.map(fetch, [(session, u) for u in urls])  # tuple list (session, url), each tuple passed to function
            executor.shutdown(wait=True)
    # write all results to text file
    with open('output.txt', 'w') as f2:
        for r in results:  # tuple (url, html)
            f2.write("%s\n" % r)
            
main()

Response file - output.txt

None    
None

Solution

First of all, you could avoid printing the html since you are saving that output to a file. That way you can avoid using resources to print the results.

Then, your fetch is not returning anything for the results. Therefore you should change your print for a return So instead of printing return the response.text

# print(response.text)
return response.text


Answered By - kachus22
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing