bellingcat / instagram-location-search

Finds Instagram location IDs near a specified latitude and longitude.
https://pypi.org/project/instagram-location-search/
MIT License
565 stars 83 forks source link

UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-13: character maps to <undefined> #25

Open wan-andrea opened 2 months ago

wan-andrea commented 2 months ago

I tried to use instagram-location-search --lat 18.78 --lng 98.98 --csv locs.csv

But it throws an error whenever it tries to bring me to the login page for Instagram.

UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-13: character maps to <undefined>
PS C:\Users\USER\downloads> instagram-location-search --lat 18.78 --lng 98.98 --csv locs.csv 

DevTools listening on ws://127.0.0.1:63333/devtools/browser/2ac137bc-ce41-4282-9c1b-bfa2566d473a
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Scripts\instagram-location-search.exe\__main__.py", line 7, in <module>
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\instagram_locations\instagram_locations.py", line 264, in main
    writer.writerow(row)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\csv.py", line 154, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
GalenReich commented 2 months ago

Hi @wan-andrea, thanks for opening this issue! It looks like this is a bug where the response from Instagram contains a character that can't be written in the default encoding.

For developers, the relevant lines are:

https://github.com/bellingcat/instagram-location-search/blob/e4045cc5b19432393c07e1270dbf5082c692d3c7/instagram_locations/instagram_locations.py#L259-L264

Changing L259 to with open(args.csv, "w", encoding="utf-8") as f: may fix this.