Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary data when saving to a file #4

Closed
ElizarovEugene opened this issue Dec 5, 2017 · 3 comments
Closed

Unnecessary data when saving to a file #4

ElizarovEugene opened this issue Dec 5, 2017 · 3 comments
Labels

Comments

@ElizarovEugene
Copy link

ElizarovEugene commented Dec 5, 2017

Thank you, after updating the problems with saving there, but when saving, there are some extra data at the beginning of the file

r = requests.get(torrent, headers={'Host':'tracktor.in'})
torrent = r.text
f = open(filename, 'w+')
f.write(bencode.bencode(torrent))
f.close()
print torrent

When the torrent data is output to the console, I see the beginning of the file as:

d8:announce76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce13:announce-listll76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceel77:http://bt99.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceee7:comment14:LostFilm.TV(c)10:created by13:uTorrent/331013:creation

but after saving to a file, the file itself has some extra data

**38149:**d8:announce76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announce13:announce-listll76:http://bt8.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceel77:http://bt99.tracktor.in/tracker.php/8e8671684b46a9a7b3b9e115c58c8984/announceee7:comment14:LostFilm.TV(c)10:created by13:uTorrent/331013:creation

I do not see in my code what this garbage could give

@fuzeman
Copy link
Owner

fuzeman commented Dec 6, 2017

You don't need to use the bencode library to download torrent files from the internet, just save the response content to disk.

Example:

r = requests.get(torrent, headers={'Host':'tracktor.in'})

f = open(filename, 'w+')
f.write(r.content)
f.close()

If you want to parse the contents of a torrent file:

r = requests.get(torrent, headers={'Host':'tracktor.in'})

torrent = bencode.bdecode(r.content)
print torrent

@ElizarovEugene
Copy link
Author

The problem is that without using it I can not save the data to a file at all

Traceback (most recent call last):
  File "index.py", line 87, in <module>
    l.getRss()
  File "index.py", line 49, in getRss
    self.getSerialPage(url)
  File "index.py", line 83, in getSerialPage
    f.write(r.text)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 535-536: ordinal not in range(128)

@fuzeman
Copy link
Owner

fuzeman commented Dec 6, 2017

Try:

f.write(r.text.encode('utf-8'))

@fuzeman fuzeman closed this as completed Jun 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants