Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import requests
from bs4 import BeautifulSoup
url = ''
categorie = [{"id":"0","nome":"Tutte le categorie"},
{"id":"29","nome":"Serie TV"},
{"id":"13","nome":"Students Releases"},
{"id":"3","nome":"E Books"},
{"id":"1","nome":"Programmi e Film TV"},
{"id":"10","nome":"Windows Software"},
{"id":"11","nome":"Pc Game"},
{"id":"12","nome":"Playstation 2"},
{"id":"21","nome":"Video Musicali"},
{"id":"27","nome":"Immagini sfondi"},
{"id":"28","nome":"Altri Giochi"},
{"id":"32","nome":"PlayStation 1"},
{"id":"33","nome":"PSP Portable"},
{"id":"38","nome":"Free Culture"}]
def parse_table(table):
if table is None: raise ValueError("table can't be none")
# Riga:
# 0: Torrent link
# 1: Magnet
# 2: Icona Categoria
# 3: Leechers
# 4: Seeders
# 5: Download Completati
# 6: Titolo
res = []
for i in range(1, len(table)):
if len(table[i]) != 7: raise ValueError("invalid row: {}".format(r))
tmp = {}
tmp['torrent'] = table[i][0].a['href'] if table[i][0].a['href'] != None else 'error'
tmp['magnet'] = table[i][1].a['href'] if table[i][0].a['href'] != None else 'error'
tmp['cat'] = table[i][2].img['src']
tmp['l'] = int(table[i][3].string)
tmp['s'] = int(table[i][4].string)
tmp['c'] = int(table[i][5].string)
titolo = table[i][6].contents
if len(titolo) != 2: return []
tmp['titolo'] = titolo[0].string.strip()
tmp['didascalia'] = titolo[1].string.strip()
return res
def ricerca(query, cat=None, page=None):
params = {
'cat': cat,
'page': page,
'srcrel': query
r =, data = params)
if not r.ok: raise ConnectionError()
soup = BeautifulSoup(r.text, 'html.parser')
t = []
for r in soup.find_all('tr'):
return parse_table(t)
def main():
print("== Inserisci testo da cercare:")
query = input()
finished = False
while not finished:
print("== In quale categoria vuoi cercare? (Premi invio per visualizzare l'elenco.)")
cat = input()
if cat == "":
for i,c in enumerate(categorie):
print("{}={}, ".format(c['id'], c['nome']))
else: finished = True
try: cat = int(cat)
except ValueError:
print("== Categoria non valida.")
n = 1
print("\n== Risultati per: {}".format(query))
results = ricerca(query, cat=cat)
if len(results) == 0:
print("== Nessun risultato.")
for i,r in enumerate(results):
print("{}) {}\n {}{}".format(i+1, r['titolo'], "" if i < 9 else " ", r['didascalia']))
print("== Digita il numero del torrent da selezionare:")
t = int(input()) - 1
print("== Dettagli del torrent")
print("\n{}\n{}\n\nTorrent:\n{}".format(results[t]['titolo'], results[t]['didascalia'], results[t]['torrent']))
return 0
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.