Мой веб-скраппер на пиратской бухте не возвращает торренты, что может быть? - PullRequest
1 голос
/ 18 июня 2020

Мой веб-скребок на пиратской бухте, не возвращает торренты, что может быть?

import requests
import lxml.html as html
import os
import datetime
import time

def thepiratebay(book):
    PB_MIRRORS = f'https://pirateproxy.surf/search.php?q={book}&all=on&search=Pirate+Search&page=0&orderby='
    LINKS_PATH = '//span[@class="list-item item-name item-title"]/a/@href'
    try:
        response = requests.get(PB_MIRRORS)
        if response.status_code == 200:
            home = response.content.decode('utf-8')
            parsed = html.fromstring(home)
            torrents = parsed.xpath(LINKS_PATH)
            complete_torrent = 'https://pirateproxy.surf'
            links_torrents = []
            for t in torrents:
                links_torrents.append(complete_torrent + t)
            print(f'THE PIRATE BAY: found {len(links_torrents)} torrents')
            return links_torrents
        else:
            raise ValueError('Error the mirror link doesnt work any more:  \n Change it in tbt.py ')
    except ValueError as ve:
        print(f'Error: {ve}')

Код не возвращает никаких торрентов, это может быть xpath, но в chrome он обнаруживает ссылки. Путь:

 //span[@class="list-item item-name item-title"]/a/@href

Результаты консоли с книжным островком: \

What book are you looking for?: small island
THE PIRATE BAY: found 0 torrents

1 Ответ

0 голосов
/ 18 июня 2020

Кажется, результат исходит из этого API:

GET https://pirateproxy.surf/api?url=/q.php?q={book}&cat=

Все ссылки выглядят так /description.php?id=28037371, а API, приведенный выше, выдает вам идентификаторы. Таким образом, вы можете использовать что-то вроде следующего:

import requests

search = "book"

r = requests.get("https://pirateproxy.surf/api",
    params = {
        "url": f"/q.php?q={search}&cat="
    })

links = [ 
    f'https://pirateproxy.surf/description.php?id={t["id"]}' 
    for t in r.json()
]
print(links)
...