简体   繁体   中英

How to write Python output to file from for loop?

I am using the Instaloader package to scrape some data from Instagram.

Ideally, I am looking to scrape the posts associated to a specific hashtag. I created the code below, and it outputs lines of scraped data, but I am unable to write this output to a .txt, .csv, or .json file.

I tried to first append the loop output to a list, but the list was empty. My efforts to output to a file have also been unsuccessful.

I know I am missing a step here, please provide any input that you have! Thank you.

    import instaloader
    import json

    L= instaloader.Instaloader()
    for posts in L.get_hashtag_posts('NewYorkCity'):
       L.download_hashtag('NewYorkCity', max_count = 10)
       with open('output.json', 'w') as f:
       json.dump(posts, f)
       break

    print('Done')

Looking at your code, it seems the spacing might be a bit off. When you use the open command with the "with" statement it does a try: / finally: on the file object.

In your case you created a variable f representing this file object. I believe if you make the following change you can write your json data to the file.

    import instaloader
    import json

    L= instaloader.Instaloader()
    for posts in L.get_hashtag_posts('NewYorkCity'):
       L.download_hashtag('NewYorkCity', max_count = 10)
       with open('output.json', 'w') as f:
           f.write(json.dump(posts))
           break

    print('Done')

I am sure you intended this, but if your goal in the break was to only get the first value in the loop returned you could make this edit as well

for posts in L.get_hashtag_posts('NewYorkCity')[0]:

This will return the first item in the list.

If you would like the first 3, for example, you could do [:3]

See this tutorial on Python Data Structures

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