brutalsavage / facebook-post-scraper

Facebook Post Scraper 🕵️🖱️
GNU General Public License v3.0
328 stars 118 forks source link

Error: 'charmap' codec can't encode characters #16

Open ShantanuPathak opened 4 years ago

ShantanuPathak commented 4 years ago

Complete noob here, so excuse my naivete

I was getting the error "'charmap' codec can't encode characters ", I googled the error and changed the code

if args.usage == "WT":
        with open('output.txt', 'w') as file:
            for post in postBigDict:
                file.write(json.dumps(post))  # use json load to recover

    elif args.usage == "CSV":
        with open('data.csv', 'w',) as csvfile:
           writer = csv.writer(csvfile)
           #writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Reaction'])
           writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Shares'])

           for post in postBigDict:
              writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Shares']])
              #writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Reaction']])

    else:
        for post in postBigDict:
            print("\n") 

to this `

 if args.usage == "WT":
        with io.open('output.txt', 'w', encoding='utf-8') as file:
            for post in postBigDict:
                file.write(json.dumps(post))  # use json load to recover

    elif args.usage == "CSV":
        with io.open('data.csv', 'w', encoding='utf-8') as csvfile:
           writer = csv.writer(csvfile)
           #writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Reaction'])
           writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Shares'])

           for post in postBigDict:
              writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Shares']])
              #writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Reaction']])

    else:
        for post in postBigDict:
            print("\n")

`

It then worked.

faizananwerali commented 4 years ago

+1 Add import io on top. Also create pull request so it gets updated for all users

ShantanuPathak commented 4 years ago

Also create pull request so it gets updated for all users

Sorry, I am not much of a coder so do not know what a pull request is

visheshaylani commented 4 years ago

encoding='utf-8' is built-in in the python 3. So, no need to add io.